ホームページ  >  記事  >  php教程  >  preg_replace は ereg_replace よりどれくらい速いですか?

preg_replace は ereg_replace よりどれくらい速いですか?

黄舟
黄舟オリジナル
2016-12-14 11:15:471309ブラウズ

preg_replace は Perl に組み込まれたテキスト マッチング モードですが、実際のプロジェクト アプリケーションでは ereg_rerace よりも使用するのが複雑なパラメータもあります。最近、HTML でテキストを取得する関数を書きました。 preg_replace は実際には ereg_replace のほぼ 2 倍高速であることがわかりました。 2 つの関数は次のとおりです。 ;||/isU","",$str);

$alltext = "";

$start = 1;

for( $i=0;$iif($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;
}

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] ==" <"){ $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;
}

多くのテストと比較の後、preg_replace を使用した関数は通常 0.08 ~ 0.12 秒の間でした。 ereg_replace を使用する関数は通常 0.08 ~ 0.12 秒かかります。テスト Web ページは Baidu のホームページです。私のシステムには Tualatin 1.1G CPU と 384M メモリが搭載されています。

プログラム内で長いテキストを処理するために ereg をまだ使用している場合は、すぐに変更することをお勧めします。

お読みいただきありがとうございます。その他の関連コンテンツについては、PHP 中国語 Web サイト (www.php.cn) に注目してください。
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。