itcdr
asked on
Remove mutiple characters from string.
I need to remove a few different characters from values returned through a mysql databases.
Characters to remove: period (.), underscore (_), single quote (')
I need to know the fastest way to do it:
1. using str_replace 3 times on each word selected from db
2. using mysql's REPLACE function 3 times when selecting from db
3. or is there a better way
Characters to remove: period (.), underscore (_), single quote (')
I need to know the fastest way to do it:
1. using str_replace 3 times on each word selected from db
2. using mysql's REPLACE function 3 times when selecting from db
3. or is there a better way
ASKER
What would be the fastest:
1. str_replace
$word=str_replace("_","",$ word);
$word=str_replace("\.","", $word);
$word=str_replace("\'","", $word);
2. REPLACE (mysql)
SELECT REPLACE(REPLACE(REPLACE(wo rd,'_','') ,'.','')," '",'') FROM ...
3. preg_replace
$patterns[0] = '/\./';
$patterns[1] = '/_/';
$patterns[2] = '/\'/';
$word=preg_replace($patter ns,"",$wor d);
4. Or is there a better way?
1. str_replace
$word=str_replace("_","",$
$word=str_replace("\.","",
$word=str_replace("\'","",
2. REPLACE (mysql)
SELECT REPLACE(REPLACE(REPLACE(wo
3. preg_replace
$patterns[0] = '/\./';
$patterns[1] = '/_/';
$patterns[2] = '/\'/';
$word=preg_replace($patter
4. Or is there a better way?
usually string functions are faster than regular expressions but maybe you should try to benchmark and see the results
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
How do I benchmark the results?
one variant :
<?php
$stimer = explode( ' ', microtime() );
$stimer = $stimer[1] + $stimer[0];
//your code
$etimer = explode( ' ', microtime() );
$etimer = $etimer[1] + $etimer[0];
echo 'Took '.($etimer-$stimer).' seconds.';
?>
<?php
$stimer = explode( ' ', microtime() );
$stimer = $stimer[1] + $stimer[0];
//your code
$etimer = explode( ' ', microtime() );
$etimer = $etimer[1] + $etimer[0];
echo 'Took '.($etimer-$stimer).' seconds.';
?>
ASKER
Thanks. It turns out the preg_replace was the fastest and the easiest.
you are welcome :)
http://fr.php.net/manual/en/function.preg-replace.php