PHP Regular (ii)

Source: Internet
Author: User
Tags preg
PHP Regular (2)

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 and non-greedy matches can be referenced in 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>)/",' \1\2\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 the regular regular replacement function is to add tags to all the links , add style to the content 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.

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