The fastest way to replace a sequence of characters in a string

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.

+1
source share
5 answers

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.

+3

str_replace .

$omschrijving = str_replace(array('(', ')'), '', $line);
+2

$omschrijving = str_replace(array('a', 'b', 'c', 'd', 'e', 'f', 'g'), "", $line);

str_replace acpets 1- 2- .

strtr, substr_replace preg_replace, - .

+1

, , preg_replace.

$string = preg_replace('/[()]/i', '', $string);

'(' ')' , - , .

, , .

$string = preg_replace('/[()-]/i', '', $string);

.

$string = preg_replace('/[()_-]/i', '', $string);

, .

+1

, 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

0
source

All Articles