Rumah > Artikel > pembangunan bahagian belakang > 辨析PHP中Ereg正则表达式_PHP教程
我们知道在PHP中Perl兼容正则表达式,那么对于Ereg正则表达式我们需要掌握什么呢?这里我们通过Perl兼容正则表达式和Perl/Ereg的区别分析来向你介绍Ereg正则表达式,希望对你有所帮助。
虽然叫做“Perl兼容正则表达式”,但是和Perl的正则表达式相比,PHP的还是由一些不同,比如修饰符“G”在Perl里面代表全部匹配,但是在PHP中没有加入对这个修饰符的支持。
关于PHP中Ereg正则表达式的辨析:
还有就是和ereg系列函数的区别,ereg也是PHP中提供的正则表达式函数,不过和preg相比,要弱上很多。
1、ereg里面是不需要也不能使用分隔符和修饰符的,所以ereg的功能比preg要弱上不少。
2、关于".":点在正则里面一般是除了换行符以外的全部字符,但是在ereg里面的"."是任意字符,即包括换行符!如果在preg里面希望"."能够包括换行符,可以在修饰符中加上"s"。
3、ereg默认使用贪婪模式,并且不能修改,这个给很多替换和匹配带来麻烦。
4、速度:这个或许是很多人关心的问题,会不会preg功能强大是以速度来换取的?不用担心,preg的速度要远远比ereg快,笔者做了一个程序测试:
PHP中Ereg正则表达式time test实例:
<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> <li class="alt"> <span class="vars">$start</span><span> = time(); </span> </li> <li> <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=1;</span><span class="vars">$i</span><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> <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> <li class="alt"><span>} </span></li> <li> <span class="vars">$ended</span><span> = time()-</span><span class="vars">$start</span><span>; </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="vars">$ended</span><span>; </span> </li> <li> <span class="func">echo</span><span> " </span> </li> <li class="alt"> <span class="func">ereg_replace</span><span> used time:"; </span> </li> <li> <span class="vars">$start</span><span> = time(); </span> </li> <li class="alt"> <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=1;</span><span class="vars">$i</span><span><span class="vars">$i</span><span>++) { </span><li> <span class="vars">$str</span><span> = </span><span class="string">"ssssssssssssssssssssssssssss"</span><span>; </span> </li> <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> <li><span>} </span></li> <li class="alt"> <span class="vars">$ended</span><span> = time()-</span><span class="vars">$start</span><span>; </span> </li> <li> <span class="func">echo</span><span> </span><span class="vars">$ended</span><span>; </span> </li> <li class="alt"> <span class="func">echo</span><span> " </span> </li> <li> <span class="func">str_replace</span><span> used time:"; </span> </li> <li class="alt"> <span class="vars">$start</span><span> = time(); </span> </li> <li> <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=1;</span><span class="vars">$i</span><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> <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> <li class="alt"><span>} </span></li> <li> <span class="vars">$ended</span><span> = time()-</span><span class="vars">$start</span><span>; </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="vars">$ended</span><span>; </span> </li> <li><span>?> </span></li></span> </li></span> </li></span> </li></span></li></ol>
PHP中Ereg正则表达式实例结果:
<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因为不需要匹配所以速度非常快,而preg_replace的速度比ereg_replace要快上不少。
PHP中Ereg正则表达式的相关内容就向你介绍到这里,希望对你了解和学习PHP中Ereg正则表达式有所帮助。