search
Homephp教程PHP开发How much faster is preg_replace than ereg_replace?

How much faster is preg_replace than ereg_replace?

Dec 14, 2016 am 11:15 AM
ereg_replacepreg_replace

preg_replace is a built-in text matching mode in Perl, but some parameters are more complicated to use than ereg_relace. In actual project applications, there are still many people using ereg. Recently, I wrote a function to obtain text in HTML. I found that preg_replace is actually nearly twice as fast as ereg_replace. The two functions are as follows:

Use preg_replace

function GetHtmlText($str)
{
$str = preg_replace("/|< ;scr(.*)/script>|/isU","",$str);
$alltext = "";
$start = 1;
for( $i=0;$iif($start==0 && $str[$i]==">") $start = 1;
else if($ start==1){
if($str[$i]=="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;$iif($start==0 && $str[$i]==">") $start = 1;
else if($start==1){
if($str[$i]==" 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 many tests and comparisons, the function using preg_replace generally takes between 0.08-0.12 seconds, and using ereg_replace The function takes between 0.35-0.38 seconds. The test webpage is Baidu's homepage. My system has a Tualatin 1.1G CPU and 384M memory.

If you still use ereg to process longer text in your program, it is recommended to change it immediately.

Thank you for reading, I hope it will be helpful to everyone. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software