PHP regular expression function preg _ PHP Tutorial

Source: Internet
Author: User
Tags php regular expression preg
The regular expression function preg _ in PHP _. Preg_match (); used for regular expression matching, and only match once preg_match_all (); used for regular expression matching, all compliant with the rule will be matched preg_replace (); positive preg_match (); // used for regular expression matching, and only matches once

Preg_match_all (); // used for regular expression matching. all matching rules are matched.

Preg_replace (); // regular expression replacement function

Preg_splite (); // regular expression division function

Preg_match (mode, string subject, arraymatches)

Here, mode is a regular rule, string subject is the string to be matched, and arraymatches is the matching result array.

Example:

[Php]

$ Mode = '/[89]/'; // Match 8 or 9

$ Str = "djfkdsjk10903990sjdfdk ";

Preg_match ($ mode, $ str, $ arr );

Print_r ($ arr );

?>

Output:

[Html]

Array ([0] => 9)

Preg_match_all (mode, string subject, arraymatches)

This function is similar to the preg_match_all () function, but it matches all the required content and stores it in a string.

Example:

[Php]

$ Mode = '/[89]/'; // Match 8 or 9

$ Str = "dj33f44k88dsjk10903990sjdfdk ";

Preg_match_all ($ mode, $ str, $ arr );

Print_r ($ arr );

?>

Output:

[Html]

Array ([0] => Array ([0] => 8 [1] => 8 [2] => 9 [3] => 9 [4] => 9))

Preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])

Use regular expressions to replace related content, similar to the previously learned str_replace string replacement, but the function is better than it.

Features: 1. the replacement content can be a regular or array regular

2. the replacement content can be replaced by the modifier e.

Purpose: replace some complicated content and use it for content conversion.

Example 1 -- array regular:

[Php]

$ Mode = array ('/{title}/', '/{author}/', '/{url }/');

$ Re = array ("code cloud", "qianshou", "http://codecloud.duapp.com /");

$ Str = "title: {title}
Author: {author}
Url: {url }";

Echo"

";

If ($ tag = preg_replace ($ mode, $ re, $ str )){

Echo $ tag;

} Else {

Echo "cannot be replaced! ";

}

?>

Output:

[Html]

Title: code cloud

Author: qianshou

Address: http://codecloud.duapp.com/

Example 2 -- replace the ubb code:

[Php]

$ Str = "Welcome to my blog: [url] http://blog.csdn.net/qsyzb#/url]";

$ Re = preg_replace ('/\ [url \] (. *) \ [\/url \]/', "\ 1", $ str );

Echo "". $ re ."";

?>

Output:

[Html

Welcome to my blog: http://blog.csdn.net/qsyzb </a>

Preg_split (string pattern, string subject [, intlimit [, int flags])

Regular expressions are used to cut related content. similar to the previously learned explode cut function, explode only has limitations in one way.

Example:

[Php]

$ Mode = '/[,. #]/';

$ Str = 'one, two. three # four ';

If ($ tag = preg_split ($ mode, $ str )){

Print_r ($ tag );

} Else {

Echo "cannot be replaced! ";

}

?>

Output:

[Html]

Array

(

[0] => one

[1] => two

[2] => three

[3] => four

)

Http://www.bkjia.com/PHPjc/477144.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477144.htmlTechArticlepreg_match (); // used for regular expression matching, and only match once preg_match_all (); // used for regular expression matching, all compliant rules will be matched with preg_replace (); // positive...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.