Master345 Master345 - 10 months ago 39
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

[A-z][0-9]
and some
. ! ?


I could do a
preg_match
, but this only verify, and I want to be replaced. This for not putting junk characters like
<p>
and
;[[;[p;[

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