Re: problems with regex

From: Date: Mon, 29 May 2006 15:32:59 +0000
Subject: Re: problems with regex
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-237027@lists.php.net to get a copy of this message
Merlin wrote:
thank you, that worked excellent! Merlin Dave Goodchild schrieb:
On 27/05/06, Merlin <ngroups@fastmail.fm> wrote:
Hi there, I am somehow lost when it comes to regex. I am trying to remove ! and ? characters from a string. Could somebody please help me to get a working regex running for that? I tried: $str = preg_replace('/\!\?\./', ' ', $str);
How about $str = preg_replace('/(\!|\?)+/', $str);
**NEVER** use regular expressions for simple search/replace operations!!! They are MUCH slower than a simple string replace, and should be avoided if at all possible. What you are doing is a very simple string replace, and you should not use regular expressions for that. I am assuming you want to remove the ! and ? and not replace them with a space. In this case: $str = str_replace(array("!". "?"), "", $str); Or if you did want to replace them with a space: $str = str_replace(array("!". "?"), " ", $str); This is especially important if you're doing the string replace in a loop, but even if you aren't, it is very bad style to use regular expressions for such a simple replacement. Regards, Adam Zey.

« previous php.general (#237027) next »