note 85718 added to function.strip-tags

From: Date: Sat, 13 Sep 2008 18:58:35 +0000
Subject: note 85718 added to function.strip-tags
Groups: php.notes 
Request: Send a blank email to php-notes+get-144402@lists.php.net to get a copy of this message
I think I may have come up with a pretty simple white-list based approach to attribute management. It's kind of hack-ish, but it's been pretty resilient with everything I've thrown at it. Check it out: function stripAttributes($string) { $string=preg_replace('/(<a.*)href=(.*>)/', '${1}href..;,;..${2}', $string); $string=preg_replace('/(<.*)id=(.*>)/', '${1}id..;,;..${2}', $string); $string=preg_replace('/(<.*)class=(.*>)/', '${1}class..;,;..${2}', $string); while(preg_match('/(<.*) .*=(\'|"|\w)\w*(\'|"|\w)(.*>)/', $string)) $string=preg_replace('/(<.*) .*=(\'|"|\w)\w*(\'|"|\w)(.*>)/', '${1}${4}', $string); $string=str_replace('..;,;..', '=', $string); return $string; } As you can see, I have it set to only allow href (in <a> tags), id, and class attributes, and everything else will be deleted. It should be pretty self-explanatory to customize it to your own purposes. ---- Server IP: 209.41.74.194 Probable Submitter: 64.178.97.149 ---- Manual Page -- http://www.php.net/manual/en/function.strip-tags.php Edit -- https://master.php.net/note/edit/85718 Del: integrated -- https://master.php.net/note/delete/85718/integrated Del: useless -- https://master.php.net/note/delete/85718/useless Del: bad code -- https://master.php.net/note/delete/85718/bad+code Del: spam -- https://master.php.net/note/delete/85718/spam Del: non-english -- https://master.php.net/note/delete/85718/non-english Del: in docs -- https://master.php.net/note/delete/85718/in+docs Del: other reasons-- https://master.php.net/note/delete/85718 Reject -- https://master.php.net/note/reject/85718 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#144402) next »