Re: Cleaning bad characters from var

From: Date: Fri, 21 Jul 2006 21:37:46 +0000
Subject: Re: Cleaning bad characters from var
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-239695@lists.php.net to get a copy of this message
John Nichel wrote:
Paul Nowosielski wrote: I wrote a function to do this for our product descriptions when sending them in a XML doc to certain vendors. It's old, crude, but you're welcome to it. <snip function that uses regular expressions for simple string replacements>
Using regular expressions for that is a very bad idea. You're using regular expressions for simple string matches, which is going to be much slower than just using the simple string replace functions. It even warns you against doing what you're doing in the manual. Here's a function that demonstrates replacing several characters with something else function replaceChars($string) {
    $find_array = array("a", "b", "c");
    $replace_array = array("apple", "banana", "carambola");
    return str_replace($find_array, $replace_array, $string);
} You can, of course, throw other string processing functions in there. Regards, Adam Zey.

« previous php.general (#239695) next »