Re: problems with regex

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Merlin wrote:
thank you, that worked excellent!

Merlin

Dave Goodchild schrieb:
On 27/05/06, Merlin <ngroups@xxxxxxxxxxx> 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.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux