preg replace - Clean string from html tags and special characters -


i want clean text html tags, html spacial characters , characters < > [ ] / \ * ,

i used $str = preg_replace("/&#?[a-za-z0-9]+;/i", "", $str); works html special characters characters doesn't remove : ( /*/*]]>*/ )

how can remove these characters?

if using php looks like, can use:

$str = htmlspecialchars($str); 

all html chars escaped (which better stripping them). if want filter these characters, need escape characters on chars list:

$str = preg_replace("/[\&#\?\]\[\/\\\<\>\*\:\(\);]*/i","",$str); 

notice there's 1 "/[]*/i", removed a-za-z0-9 should want these chars in. can classify desired chars enter string (will give trouble accentuations á é ü if use them, have specify every accepted char):

$str = preg_replace("/[^a-za-z0-9áÁéÉíÍãÃüÜõÕñÑ\.\+\-\_\%\$\@\!\=;]*/","",$str); 

notice there's never escape characters, unless example intervals (\a-\z fine, \a-\z match a, or -, or z).

i hope helps. :)


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -