I am wondering what is the fastest way to replace some characters in a string with nothing. Now I have the following:
$omschrijving = str_replace(")", "", str_replace("(", "", $line)));
Is there a better way to do this? This replaces only "("and ")", but what if I want to do the same with a lot of characters. Still learning the methods preg_, but at the moment still a little bloated.
"("
")"
preg_
Here you go:
str_replace(array('a', 'b', 'c', 'd'), '', $sString);
From the manual :
A replacement value that replaces the found search values. an array can be used to indicate several replacements.
str_replace .
str_replace
$omschrijving = str_replace(array('(', ')'), '', $line);
$omschrijving = str_replace(array('a', 'b', 'c', 'd', 'e', 'f', 'g'), "", $line);
str_replace acpets 1- 2- .
strtr, substr_replace preg_replace, - .
strtr
substr_replace
preg_replace
, , preg_replace.
$string = preg_replace('/[()]/i', '', $string);
'(' ')' , - , .
, , .
$string = preg_replace('/[()-]/i', '', $string);
.
$string = preg_replace('/[()_-]/i', '', $string);
, .
, str_replace 1 2 . 3 , preg_replace .
I used this testmark protocole , modifying it so that it accepts different characters instead of one.
For 3 characters, here are the results:
Time for str_replace: 1.919958114624 seconds Time for preg_replace: 1.4596478939056 seconds