: 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) ;}) ;}; scriptThe 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.