The PHP chapter REPLACE,FILTER,GREP of regular expressions

Source: Internet
Author: User
Tags preg
 - preg\_replace( \$pattern,\$replacement,\$subject),preg\_filter ( \$pattern,\$replacement,\$subject); preg\_replace 和preg\_filter 将匹配到的数值进行替换,可以使用数组替换;\ $replacement 将匹配到的值替换为这个变量中存放的值; \$pattern正则表达式; \$subject返回结果;

Difference:

functionshow($var = null){if(empty($var)){ echo'null'; }elseif(is_array($var)||is_object($var)){ echo'
'; Print_r ($var);Echo'
'; }Else{Echo$var; }}//preg_replace,preg_filter//$pattern = '/[0-9]/'; //String$pattern=Array('/[0123]/','/[456]/','/[789]/');//Array//$subject = ' y1jp78yn16ww55j9 ';//is a string, using $pattern and $replacement to make a match substitution$subject=Array(' y ',' 1jp78 ',' yn ',' 16WW ',' 55j9 ');//$subject string is arbitrarily split into several segments, placed in the $subject array, each of the values in the group is replaced by a match//$replacement = ' mu goddess ';//the variable to be replaced when $subject matches the result$replacement=Array(' Mu ',' Woman ',' God ');$str 1= Preg_replace ($pattern,$replacement,$subject);//Keep all strings$str 2= Preg_filter ($pattern,$replacement,$subject);//Only preserves strings that have been replacedShow$str 1);Echo'
       '; Show ($str 2);

Results:

Conclusion:

preg\_replace在数组匹配中会输出未发生匹配的字段;preg\_filter 在数组匹配中不会输出未发生匹配的字段;
    • Preg_grep ($pattern,array $input)
      The function is similar to Preg_filter, only matches does not replace and only returns the string or array that matches, can act as a filter, filter out the string or array that preg_filter not replace.

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the regular expression of PHP replace,filter,grep, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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