Link to home
Start Free TrialLog in
Avatar of itcdr
itcdrFlag for United States of America

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
Avatar of alextr2003fr
alextr2003fr

Avatar of itcdr

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(word,'_',''),'.',''),"'",'') FROM ...

3. preg_replace
$patterns[0] = '/\./';
$patterns[1] = '/_/';
$patterns[2] = '/\'/';
$word=preg_replace($patterns,"",$word);

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
Avatar of alextr2003fr
alextr2003fr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of itcdr

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.';
?>
Avatar of itcdr

ASKER

Thanks. It turns out the preg_replace was the fastest and the easiest.
you are welcome :)