Home  >  Article  >  Backend Development  >  Analyzing Ereg regular expressions in PHP_PHP tutorial

Analyzing Ereg regular expressions in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:26:29984browse

We know that Perl is compatible with regular expressions in PHP, so what do we need to know about Ereg regular expressions? Here we introduce Ereg regular expressions to you through analysis of the differences between Perl-compatible regular expressions and Perl/Ereg. We hope it will be helpful to you.

Although it is called "Perl compatible regular expression", compared with Perl's regular expression, PHP's still has some differences. For example, the modifier "G" in Perl means all matches, but in PHP No support for this modifier has been added.

About the analysis of Ereg regular expressions in PHP:

There is also the difference from the ereg series of functions. ereg is also a regular expression function provided in PHP, but Compared with preg, it is much weaker.

1. Separators and modifiers are not required and cannot be used in ereg, so the function of ereg is much weaker than preg.

2. About ".": The dot in the regular expression is usually all characters except the newline character, but the "." in the ereg is any character, including the newline character! If you want "." to include a newline character in preg, you can add "s" to the modifier.

3. ereg uses greedy mode by default and cannot be modified. This brings trouble to many replacements and matchings.

4. Speed: This may be a question that many people are concerned about. Is the powerful function of preg in exchange for speed? Don’t worry, preg is much faster than ereg. The author made a program test:

Ereg regular expression time test example in PHP:

<ol class="dp-c"><li class="alt"><span><span><?php  </SPAN></SPAN><LI><SPAN class=func>echo</SPAN><SPAN> </SPAN><SPAN class=string>"Preg_replace used time:"</SPAN><SPAN>;   </SPAN><LI class=alt><SPAN class=vars>$start</SPAN><SPAN> = time();   </SPAN><LI><SPAN class=keyword>for</SPAN><SPAN>(</SPAN><SPAN class=vars>$i</SPAN><SPAN>=1;</SPAN><SPAN class=vars>$i</SPAN><SPAN><=100000;</SPAN><SPAN class=vars>$i</SPAN><SPAN>++) {   </SPAN><LI class=alt><SPAN class=vars>$str</SPAN><SPAN> = </SPAN><SPAN class=string>"ssssssssssssssssssssssssssss"</SPAN><SPAN>;   </SPAN><LI><SPAN>preg_replace(</SPAN><SPAN class=string>"/s/"</SPAN><SPAN>,</SPAN><SPAN class=string>""</SPAN><SPAN>,</SPAN><SPAN class=vars>$str</SPAN><SPAN>);   </SPAN><LI class=alt><SPAN>}   </SPAN><LI><SPAN class=vars>$ended</SPAN><SPAN> = time()-</SPAN><SPAN class=vars>$start</SPAN><SPAN>;   </SPAN><LI class=alt><SPAN class=func>echo</SPAN><SPAN> </SPAN><SPAN class=vars>$ended</SPAN><SPAN>;   </SPAN><LI><SPAN class=func>echo</SPAN><SPAN> "   </SPAN><LI class=alt><SPAN class=func>ereg_replace</SPAN><SPAN> used time:";   </SPAN><LI><SPAN class=vars>$start</SPAN><SPAN> = time();   </SPAN><LI class=alt><SPAN class=keyword>for</SPAN><SPAN>(</SPAN><SPAN class=vars>$i</SPAN><SPAN>=1;</SPAN><SPAN class=vars>$i</SPAN><SPAN><=100000;</SPAN><SPAN class=vars>$i</SPAN><SPAN>++) {   </SPAN><LI><SPAN class=vars>$str</SPAN><SPAN> = </SPAN><SPAN class=string>"ssssssssssssssssssssssssssss"</SPAN><SPAN>;   </SPAN><LI class=alt><SPAN class=func>ereg_replace</SPAN><SPAN>(</SPAN><SPAN class=string>"s"</SPAN><SPAN>,</SPAN><SPAN class=string>""</SPAN><SPAN>,</SPAN><SPAN class=vars>$str</SPAN><SPAN>);   </SPAN><LI><SPAN>}   </SPAN><LI class=alt><SPAN class=vars>$ended</SPAN><SPAN> = time()-</SPAN><SPAN class=vars>$start</SPAN><SPAN>;   </SPAN><LI><SPAN class=func>echo</SPAN><SPAN> </SPAN><SPAN class=vars>$ended</SPAN><SPAN>;   </SPAN><LI class=alt><SPAN class=func>echo</SPAN><SPAN> "   </SPAN><LI><SPAN class=func>str_replace</SPAN><SPAN> used time:";   </SPAN><LI class=alt><SPAN class=vars>$start</SPAN><SPAN> = time();   </SPAN><LI><SPAN class=keyword>for</SPAN><SPAN>(</SPAN><SPAN class=vars>$i</SPAN><SPAN>=1;</SPAN><SPAN class=vars>$i</SPAN><SPAN><=100000;</SPAN><SPAN class=vars>$i</SPAN><SPAN>++) {   </SPAN><LI class=alt><SPAN class=vars>$str</SPAN><SPAN> = </SPAN><SPAN class=string>"sssssssssssssssssssssssssssss"</SPAN><SPAN>;   </SPAN><LI><SPAN class=func>str_replace</SPAN><SPAN>(</SPAN><SPAN class=string>"s"</SPAN><SPAN>,</SPAN><SPAN class=string>""</SPAN><SPAN>,</SPAN><SPAN class=vars>$str</SPAN><SPAN>);   </SPAN><LI class=alt><SPAN>}   </SPAN><LI><SPAN class=vars>$ended</SPAN><SPAN> = time()-</SPAN><SPAN class=vars>$start</SPAN><SPAN>;   </SPAN><LI class=alt><SPAN class=func>echo</SPAN><SPAN> </SPAN><SPAN class=vars>$ended</SPAN><SPAN>;   </SPAN><LI><SPAN>?>  </span></span></li></ol>

Ereg regular expression example results in PHP:

<ol class="dp-c">
<li class="alt"><span><span>Preg_replace used time:5   </span></span></li>
<li>
<span class="func">ereg_replace</span><span> used time:15   </span>
</li>
<li class="alt">
<span class="func">str_replace</span><span> used time:2  </span>
</li>
</ol>

str_replace is very fast because it does not require matching, and preg_replace is much faster than ereg_replace.

This concludes the introduction to Ereg regular expressions in PHP. I hope it will be helpful for you to understand and learn Ereg regular expressions in PHP.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446583.htmlTechArticleWe know that Perl is compatible with regular expressions in PHP, so what do we need to know about Ereg regular expressions? Here we analyze the difference between Perl compatible regular expressions and Perl/Ereg...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn