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...