ohdang888 Posted March 21, 2009 Share Posted March 21, 2009 I want to strip content of certain html, and of the allowable html, i want to search through the attributes. I found the script below on php.net I have content set as this: test<img src="http://www.google.com/logos/spring09.gif" title="cool"><a href="#" Onclick="test();">test</a> and it only echos this: test<img src="http://www.google.com/logos/spring09.gif"> In other words, it comes across an attribute it doesn't allow, and it stops every bit of html after that point, instead of just striping that html. i obviosuly don't like that. How can i change it? Thanks What i am using: <?php echo strip_tags_attributes($contents,'<br><b><u><span><div><table><tr><td><strong><em><a><img><form><input>','href,rel,src,width,height,color,size'); ?> The script: <?php function strip_tags_attributes($string,$allowtags=NULL,$allowattributes=NULL){ if($allowattributes){ if(!is_array($allowattributes)) $allowattributes = explode(",",$allowattributes); if(is_array($allowattributes)) $allowattributes = implode("|",$allowattributes); $rep = '/([^>]*) ('.$allowattributes.')(=)(\'.*\'|".*")/i'; $string = preg_replace($rep, '$1 $2_$4', $string); } $string = preg_replace('/([^>]*) (.*)(=\'.*\'|=".*")(.*)/i', '$1$4', $string); $rep = '/([^>]*) ('.$allowattributes.')(_)(\'.*\'|".*")/i'; if($allowattributes) $string = preg_replace($rep, '$1 $2=$4', $string); return strip_tags($string,$allowtags); } ?> Link to comment https://forums.phpfreaks.com/topic/150432-strip-tags-functions/ Share on other sites More sharing options...
ohdang888 Posted March 21, 2009 Author Share Posted March 21, 2009 opps, accidently pressed quote, rather than modify, now this post too is modified. blah. Link to comment https://forums.phpfreaks.com/topic/150432-strip-tags-functions/#findComment-790087 Share on other sites More sharing options...
ohdang888 Posted March 21, 2009 Author Share Posted March 21, 2009 bump Link to comment https://forums.phpfreaks.com/topic/150432-strip-tags-functions/#findComment-790512 Share on other sites More sharing options...
cps Posted April 23, 2010 Share Posted April 23, 2010 Hello, I need a function like a strip_tags_attributes() and I`ve found this version, and... it has a bug... when you want accept usig <b> and you write: echo strip_tags_attributes($q,'<b>'); user can use JavaScript in onclick etc when he doesn`t use comma f.e.: <b onclick=alert('w')>testik</b> I know this is mistake of browser, but e.g. in my Opera 10.51 this hack is working in Chrome 4.1 also... I suppose that in all of browsers that will be working Link to comment https://forums.phpfreaks.com/topic/150432-strip-tags-functions/#findComment-1047384 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.