What are the regular expression functions in PHP? So what does the PHP regular expression function specifically represent? What is the specific usage? Let us introduce you one by one. PHP regular expressions are mainly used for processing complex strings. Its main PHP regular expression functions are as follows:
◆ereg()
◆ereg_replace()
◆eregi()
◆eregi_replace()
◆split()
Summary of PHP regular expression function usage:
(1)ereg, eregi
This is a regular expression matching function. The former is case-related matching, while the latter is irrelevant.
Usage:
ereg(regular expression, string, [ Match part of the array name]); The regular expressions in PHP3.0 are roughly similar to those used in grep.
(2)ereg_replace, eregi_replace
These are replacements Function.
Usage:
ereg_replace (regular expression, replacement string, original string);
There is a strtr in the string processing function, which is the "translation" function , similar to tr/.../.../ in Perl,
usage:
strtr (string, "from", "to");
For example:
strtr("aaabb","ab","cd") returns "cccdd".
(3)split
It is somewhat similar to the explode function, but this time you can split the string where it matches a certain regular expression.
Usage:
split (regular expression, string, [take out the first number of items ]);
These functions all use regular strings as the first parameter. PHP uses extended regular strings as defined by the Posix 1003.2 standard. For a complete description of Posix regular expressions, see the man page in the regex directory of the PHP package.
PHP regular expression function application examples:
<ol class="dp-c"> <li class="alt"><span><span class="func">ereg</span><span>(</span><span class="string">"abc"</span><span>,</span><span class="vars">$string</span><span>); </span></span></li> <li> <span class="comment">/* Returns true if "abc" is </span> </li> <li class="alt"><span><span class="comment">found anywhere in $string. */</span><span> </span></span></li> <li><span> </span></li> <li class="alt"> <span class="func">ereg</span><span>(</span><span class="string">"^abc"</span><span>,</span><span class="vars">$string</span><span>); </span> </li> <li> <span class="comment">/* Returns true if "abc" is </span> </li> <li class="alt"><span><span class="comment">found at the beginning of $string. */</span><span> </span></span></li> <li><span> </span></li> <li class="alt"> <span class="func">ereg</span><span>(</span><span class="string">"abc$"</span><span>,</span><span class="vars">$string</span><span>); </span> </li> <li> <span class="comment">/* Returns true if "abc" is </span> </li> <li class="alt"><span><span class="comment"> found at the end of $string. */</span><span> </span></span></li> <li><span> </span></li> <li class="alt"> <span class="func">eregi</span><span>(</span><span class="string">"(ozilla.[23]|MSIE.3)"</span><span>,</span><span class="vars">$HTTP_USER_AGENT</span><span>); </span> </li> <li> <span class="comment">/* Returns true if client browser </span> </li> <li class="alt"><span><span class="comment"> is Netscape 2, 3 or MSIE 3. */</span><span> </span></span></li> <li><span> </span></li> <li class="alt"> <span class="func">ereg</span><span>("([[:alnum:]]+) ([[:alnum:]]+) </span> </li> <li> <span> ([[:alnum:]]+)",</span><span class="vars">$string</span><span>,</span><span class="vars">$regs</span><span>); </span> </li> <li class="alt"> <span class="comment">/* Places three space separated words </span> </li> <li><span><span class="comment">into $regs[1], $regs[2] and $regs[3]. */</span><span> </span></span></li> <li class="alt"><span> </span></li> <li> <span class="func">ereg_replace</span><span>(</span><span class="string">"^"</span><span>,</span><span class="string">""</span><span>,</span><span class="vars">$string</span><span>); </span> </li> <li class="alt"> <span class="comment">/* Put a tag at the beginning of $string. */</span><span> </span> </li> <li><span> </span></li> <li class="alt"> <span class="func">ereg_replace</span><span>(</span><span class="string">"$"</span><span>,</span><span class="string">""</span><span>,</span><span class="vars">$string</span><span>); </span> </li> <li> <span class="comment">/* Put a tag at the end of $string. */</span><span> </span> </li> <li class="alt"><span> </span></li> <li> <span class="func">ereg_replace</span><span>(</span><span class="string">" "</span><span>,</span><span class="string">""</span><span>,</span><span class="vars">$string</span><span>); </span> </li> <li class="alt"> <span class="comment">/* Get rid of any carriage </span> </li> <li><span><span class="comment">return characters in $string. */</span><span> </span></span></li> </ol>
The relevant content of PHP regular expression function is introduced to you here, I hope you can understand and learn from it PHP regular expression functions help.