Regular string replacement and partition function comparison in PHP
this article introduces the differences between the regular regular matching functions of PHP, including str_replace, str_ireplace, substr_replace, preg_replace, preg_match, preg_match_all, preg_quote, preg_split, ereg_replace, preg_match, etc. Of course, a few of them can't use regular expressions, but because they are ambiguous in relation to the related regular functions, they can be put together for comparison.
name supports regular feature notes
str_replace X string substitution function, case sensitive
str_ireplace X string substitution function, case insensitive, support array batch replacement thanks to netizen Franci, remind add
substr_replace X part replace string function, can refer to Location is replaced by index
preg_replace Y specified matching mode, support substring reference is replaced by
ereg_replace Y specified matching mode, case sensitive, support substring reference
eregi_replace Y specifies matching mode to be replaced, case insensitive, support substring Reference
ereg Y specified pattern full text matching, can be used to match judgment, or return a matching array
preg_match Y to specify pattern matching one exit, can be used to match judgment, or use the returned matching array to specify mode full text matching with
preg_match_all Y. Use the matched array of returns to use
preg_split Y to specify a regular split in the matching mode, if it is best to use explode or str_split
str_split X to specify a length dissection string, by default a single character is divided into an array
explode X to specify a single or more. Character dissection string, the success returns an array, for example 12345 returns 12 and 5
preg_quote - canonical expression characters according to the 34 section, meaning the special character is added backslash, and the special characters of the regular expression include: *? [^] $() {} =!.lt;.Gt;.Gt;
Add:
1, there are two sets of regular expression library in PHP. The function is very similar:
a set is provided by POSIX (Portable Operating System) library, the function is named after the prefix, and the regular function library is no longer recommended.
is provided by PCRE (Perl Compatible Regular Expression) library. The function is named by preg_ prefix and recommended for priority use. In PCRE, mode expressions (i.e. regular expressions) are usually included between the two backslashes "/", such as "/^w $/" style.
2, PHP regular expression function: matching extraction, matching and replacement.