Master345 Master345 -3 years ago 136
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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download