matt matt - 1 year ago 46
PHP Question

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

hey guys,
simple question... Is this the best way to do it?

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

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

I have three search-patterns I want to filter out! Is my code above appropriate or is there a better way?

Thank you for the info


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.