Home > Backend Development > PHP Tutorial > Regular expression function preg__PHP tutorial in PHP

Regular expression function preg__PHP tutorial in PHP

WBOY
Release: 2016-07-15 13:21:42
Original
1134 people have browsed it

preg_match(); //Used for regular expression matching, and only matches once

preg_match_all(); // Used for regular expression matching, all matches that match the rules will be matched
preg_replace(); //Regular expression replacement function
preg_splite(); //Regular split function
preg_match (mode, string subject, arraymatches)
where mode is a regular rule, string subject is the string to be matched, and arraymatches is the array of matching results
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 this function will match all content that meets the requirements and store 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] )
Replace related content through regular expressions, similar to the str_replace string replacement learned before, but the function is stronger than it.
Features: 1. The replacement content can be a regular expression or an array regular expression
2. The replacement content can be solved by using the modifier e to replace the execution content
Usage: to replace some more complex content, and can also be used for content conversion
Example 1 - Array regularization:
[php]
$mode=array('/{title}/','/{author}/','/{url}/');
$re=array("code cloud","qianshou","http://codecloud.duapp.com/");
$str="Title: {title}
Author: {author}
Address: {url}";
echo "
";
if($tag=preg_replace($mode,$re,$str)){
echo $tag;
}else{
echo "Replacement failed!";
}
?>
Output:
[html]
Title: code cloud
Author: qianshou
Address: http://codecloud.duapp.com/
Example 2 - Replacement of 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
preg_split ( string pattern, string subject [, intlimit [, int flags]] )
Cut related content through regular expressions, similar to the explode cutting function learned before, but explode can only cut in one way and has limitations.
Example:
[php]
$mode='/[,.#]/';
$str='one,two.three#four';
if($tag=preg_split($mode,$str)){
print_r($tag);
}else{
echo "Replacement failed!";
}
?>
Output:
[html]
Array
(
[0] => one
[1] => two
[2] => three
[3] => four
)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477144.htmlTechArticlepreg_match(); //Used for regular expression matching, and only matched once preg_match_all();// Used for regular expression matching, all matching rules will be matched preg_replace(); //Regular...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template