I want to move some char eg.(/,\,/t..etc) from mysql result. Can I strip every char with one function? I want to know this function.Help me!
+2
A:
You can replace the occurences with the empty string, str_replace()
allows you to do multiple replace operations at once:
$string = str_replace(array("\t", '/', '\\'), '', $string);
soulmerge
2009-07-27 09:58:46
The second parameter doesn't need to be an array: "If search is an array and replace is a string, then this replacement string is used for every value of search" - http://php.net/manual/en/function.str-replace.php
Tom Haigh
2009-07-27 10:39:48
Thanks, didn't know that. I'll update the answer.
soulmerge
2009-07-27 10:47:11
A:
To remove a set of characters from a string:
function removeBadCharacters($input) {
// removes '/', '\\' '\t'
return preg_replace("@[".preg_quote("/\\\t", '@')."]@g", '', $input);
}
Matthew Scharley
2009-07-27 10:00:34
Please don't use regular expressions for such trivial tasks, you'll make your code less readable, slower, error-prone and teach others bad practice. Example error: If you decide to escape the at-sign later-on you will run into trouble. You should preg_quote() the values.
soulmerge
2009-07-27 10:05:30