views:

73

answers:

2

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
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
Thanks, didn't know that. I'll update the answer.
soulmerge
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
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
+1 for teaching me a new function, didn't know about that one.
Matthew Scharley