How fast is preg_replace than ereg_replace?

Source: Internet
Author: User

 

Preg_replace is a built-in text match mode in Perl. However, some parameters are more complex than ereg_relace. In actual projects, there are still a lot of people using ereg, recently I wrote a function to get the text in HTML, and found that preg_replace is nearly twice faster than ereg_replace. The two functions are as follows:

Use preg_replace

Function GetHtmlText ($ str)
{
$ Str = preg_replace ("/ | | /IsU "," ", $ str );
$ Alltext = "";
$ Start = 1;
For ($ I = 0; $ I If ($ start = 0 & $ str [$ I] = ">") $ start = 1;
Else if ($ start = 1 ){
If ($ str [$ I] = "<") {$ start = 0; $ alltext. = "";}
Else if (ord ($ str [$ I])> 32) $ alltext. = $ str [$ I];
}
}
$ Alltext = preg_replace ("/& ([^; &] *) (; | &)/", "", $ alltext );
$ Alltext = preg_replace ("/{1,}/", "", $ alltext );
$ Alltext = preg_replace ("/{1,}/", "", $ alltext );
Return $ alltext;
}

Use ereg_replace

Function GetHtmlText ($ str)
{
$ Str = eregi_replace (" | | "," ", $ Str );
$ Alltext = "";
$ Start = 1;
For ($ I = 0; $ I If ($ start = 0 & $ str [$ I] = ">") $ start = 1;
Else if ($ start = 1 ){
If ($ str [$ I] = "<") {$ start = 0; $ alltext. = "";}
Else if (ord ($ str [$ I])> 32) $ alltext. = $ str [$ I];
}
}
$ Alltext = ereg_replace ("& ([^; &] *) (; | &)", "", $ alltext );
$ Alltext = ereg_replace ("{1,}", "", $ alltext );
$ Alltext = ereg_replace ("{1,}", "", $ alltext );
Return $ alltext;
}

After multiple tests and comparisons, the functions using preg_replace are generally between 0.08-0.12 seconds. The functions using ereg_replace go between 0.35-0.38 seconds. The test webpage is Baidu's homepage, my system has a CPU of GB and a memory of MB.

If your program still uses ereg to process long text, we recommend that you change it immediately.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.