faqts : Computers : Programming : Languages : PHP : Common Problems : Regular Expressions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

19 of 26 people (73%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How can I remove unwanted characters from a string?

Jul 9th, 2003 13:07
Gerhardt, Nathan Wallace, matt, Sascha Schumann


Solution with str_replace (not tested):
---------------------------------------
$unwanted = Array('a','b','c');
$text = str_replace($unwanted,'',$text);
Solution with Regular Expression:
---------------------------------
You can use more than one argument to ereg_replace in the same line lie
so:
    $message=ereg_replace("([^%)#&?])","",$message);
The PHP distribution contains a man page which discusses POSIX
style regular expressions. It is in php-3.0.x/regex/regex.7. To
read it, use
nroff -man php-3.0.x/regex/regex.7 | less
If you prefer Perl-style regular expressions, read on at
http://www.php.net/manual/ref.pcre.php3