regex - php string replacement performance -


i know suggested avoid using regular expressions wherever possible, if there list of 20 or characters need strip form string?

would more cost effective

$string = "...";  $a = array('a', '1', '!' ...);  foreach($a $char){     $string = str_replace($char, '', $string);  } 

or better go regular expression

$string = preg_replace('#[a1!...]#', '', $string); 

thank you!

first off str_replace() supports arrays both needle , haystack running through loop process.. i'd in reply regex vs string vs string. i'd go string string in cases. regex can give false positives, , positive negatives. if not formulated correctly, , in such can become frustrating quickly. using sparingly, regex has adds more process has match or not match rule given. string string if matches matches.

but opinion on matter.


Comments