PHP Regular
There are two more important methods of Preg_match () and preg_replace (); Give a simple example. To match this, use the following pattern
$str"123123,123123,123123,213123,234234";$model"/^([0-9]*),(([0-9]*),))*([0-9]*)$/";$matcharray();$ret = preg_match($model$str$match);$match 中就有匹配到的内容。
Greedy MatchAnd
non-greedy matchYou can refer to the following example
$eee="( -123) 23-(233)";//Greedy Match example this pattern can only match one. $preg="/\(.*\)/";$match=Array();$ans= Preg_match_all ($preg,$eee,$match); Var_dump ($match);//The result is that only one pattern can be matched. Because the first one is the whole, it's a complete parenthesis, so it matches the whole result. //non-greedy matching mode, you can find all the parentheses in the content, but not the question mark behind the *. $preg="/\(.*?\)/";$match=Array();$ans= Preg_match_all ($preg,$eee,$match); Var_dump ($match);
Regular replacement
preg_replace () can replace the contents of $str. Or the above example, this time we replace the number in parentheses of the Eee Chinese year with the string haha. You can design the following methods. Pay attention to the use of preg_replace . $pattern and Replace are all arrays, and the following are the specific matching patterns.
按照正则表达式的规则对匹配的内容进行替换。$eee"(12)-123 23-(233)";$pregarray("/\(.*?\)/");$replacearray("haha");$ans = preg_replace($preg,$replace,$eee);var_dump($ans);
The above is a relatively basic usage, the following is slightly more complicated.
$str= Preg_replace ("/(<a.*?>) (. *?) (<\/a>)/",' \1<span class= ' link ' >\2</span>\3 ',$str);//With three sub-patterns (one sub-pattern in each parenthesis), the first is the link start tag and the second is the link text//The third is </a> this regular regular replacement function is to add a <span> tag to all the links, adding a style to the contents of the link. //By learning the above expression is, we can turn our previous examples into this. $eee="whie2ch ( -123) 23-function (233)";$preg="/(\w*) \ (. *?\)/";$replace="\1WORD\2BB";//Change the variable to Word, and replace the brackets and parentheses with the BB \1 \2 to represent the contents of the first few parentheses. $ans= Preg_replace ($preg,$replace,$eee); Var_dump ($ans);
PHP modifier
$eee ="whie2ch(1 \n2)-123 n 23-function nhehe(23 \n3) sfsdf(24 \n5)";$preg ="/\(.*?\)/";//$replace ="\1word\2bb";$matcharray();$ans =preg_match_all($preg,$eee,$match);var_dump($match);// 这样,发现是匹配不到括号里面的内容的。因为.不包括换行。// 使用方法,把上面的 $preg 改成下面这样。$preg ="/\(.*?\)/s";重新运行程序,发现所有的内容都匹配出来了。
Several modifiers for PHP are as follows
| modifier
meaning |
S |
Let. Include line breaks, you can implement cross-line matching |
U |
Make the default match into a greedy state |
I |
Ignore case |
U |
The pattern string is treated as UTF8 |
M |
When this modifier is set, the line start (^) and line end ($) match the beginning and end of the entire string, respectively, after the line break (\ n) and before the |
The php modifier is used as shown in the program above. Will /your_regrex/X
, where x represents your modifier and is selected as needed.
PHP Regular (2)