Regular expressions in PHP replace, filter, grep

Source: Internet
Author: User
Tags preg
: This article mainly introduces regular expressions in PHP replace, filter, and grep. For more information about PHP tutorials, see.
-Preg \ _ replace (\ $ pattern, \ $ replacement, \ $ subject), preg \ _ filter (\ $ pattern, \ $ replacement, \ $ subject ); preg \ _ replace and preg \ _ filter replace the matched values with arrays. \ $ replacement replaces the matched values with the values stored in the variable; \ $ pattern regular expression; \ $ subject return result;

Differences:

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'; // string. replace $ subject = array ('Y ', '1json78 ', 'yn', '16ww', '55j9'); // splits the $ subject string into several segments and stores them in the $ subject array, replace each value in the array with a match. // $ replacement = 'Goddess of Mu '; // variable to be replaced after $ subject matches the result $ replacement = array ('mu', 'female, 'shen'); $ str1 = preg_replace ($ pattern, $ replacement, $ subject); // keep all strings $ str2 = preg_filter ($ pattern, $ replacement, $ subject ); // only the string show ($ str1); echo that has been replaced will be retained'
    '; Show ($ str2 );

Result:

Conclusion:

Preg \ _ replace outputs unmatched fields in array matching. preg \ _ filter does not output unmatched fields in array matching;
  • Preg_grep ($ pattern, array $ input)
    Similar to preg_filter, this function only matches strings or arrays that do not replace but only return matched strings or arrays. it can be used as a filter to filter out strings or arrays that do not replace preg_filter.

'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
  • '). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script

    The above describes the regular expression of PHP replace, filter, grep, including some content, hope to be helpful to friends who are interested in PHP tutorials.

    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.