PHP preg_replace three times with three different patterns? right or wrong?

后端 未结 5 1815
北海茫月
北海茫月 2020-12-30 07:43

A simple question: Is this the best way to do it?

$pattern1 = \"regexp1\";
$pattern2 = \"regexp2\";
$pattern3 = \"regexp3\";

$content = preg_replace($patter         


        
5条回答
  •  长情又很酷
    2020-12-30 08:17

    As you are replacing all with the same, you could do either pass an array

    $content = preg_replace(array($pattern1,$pattern2, $pattern3), '', $content);
    

    or create one expression:

    $content = preg_replace('/regexp1|regexp2|regexp3/', '', $content);
    

    If the "expressions" are actually pure character strings, use str_replace instead.

提交回复
热议问题