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.