Master345 Master345 - 5 months ago 23
PHP Question

PHP filter with preg_replace allow only letters

I have a little problem with preg_replace. I need a function that converts all characters to nothing except

and some
. ! ?

I could do a
, but this only verify, and I want to be replaced. This for not putting junk characters like

in description META TAG.

So the script must be like:

;")<br>kk23?! => brkk23?!

Any help would be appreciated :D

Answer Source
$string = ';")<br>kk23?!'; 
$new_string = preg_replace("/[^A-Za-z0-9?!]/",'',$string);
echo $new_string;

leave- letters, numbers, spaces, ?!

/* 3 choices. Pick one you like! */
$str = preg_replace("/[^A-Za-z0-9?! ]/","",$str);
$str = preg_replace("/[^A-Za-z0-9?!\s]/","",$str);
$str = preg_replace("/[^A-Za-z0-9?![:space:]]/","",$str);