Page 1 of 1

Remove chars

Posted: Fri Dec 24, 2010 10:36 am
by JKM
Hi there,

what is the best way to remove all chars but:[text]1-9a-Z|-_.,#%(){}[]~^+?![/text]
If regex is the way to go, could anyone please give me the regex sentence I should use?

Thanks!
Merry xmas!

Re: Remove chars

Posted: Sat Dec 25, 2010 5:05 am
by Darhazer
Try this one:

Code: Select all

$charset = '1-9a-zA-Z\|\-\_\.\,\#\%\(\)\{\}\[\]\~\^\+\?\!';
$string = '@12345678900-=+_aAbBcC';
$result = preg_replace('/[^'.$charset.']/i', '', $string);

Re: Remove chars

Posted: Sat Dec 25, 2010 4:53 pm
by s.dot
You don't need regular expressions, but it will work.