Arty Ziff Posted March 26, 2011 Share Posted March 26, 2011 It seems like there should be a dedicated php function to strip a SPECIFIC tag, rather than the functionality of strip_tags. I'm using this: function stripSingleTags($tags, $string) { foreach( $tags as $tag ) { $string = preg_replace('#</?'.$tag.'[^>]*>#is', '', $string); } return $string; } But this will not help me if for example I want to trip out scripts <script> ... </script>. Is there a better way? Link to comment https://forums.phpfreaks.com/topic/231791-stripping-tags/ Share on other sites More sharing options...
sunfighter Posted March 26, 2011 Share Posted March 26, 2011 You can use the preg_replace() . Link to comment https://forums.phpfreaks.com/topic/231791-stripping-tags/#findComment-1192609 Share on other sites More sharing options...
mattal999 Posted March 26, 2011 Share Posted March 26, 2011 Well, if you take a look at the documentation of strip_tags(), then you'd see: string strip_tags ( string $str [, string $allowable_tags ] ) You can pass an string of the allowed tags that it will leave unaffected. This is the simplest way to have the function, as you would much rather have a whitelist then a blacklist. Blacklisting requires constant updating, and the list of tags would be very long. Here's an example: echo strip_tags("<p><script>alert('Hello World');</script>I don't like <a href='#'>Javascript</a></p><ul><li>This is a list.</li></ul>", "<p><a>"); Would output: <p>I don't like <a href='#'>Javascript</a></p> Link to comment https://forums.phpfreaks.com/topic/231791-stripping-tags/#findComment-1192629 Share on other sites More sharing options...
Arty Ziff Posted March 26, 2011 Author Share Posted March 26, 2011 Thanks, Mattal999, sure I thought about that, but out of the huge number of possible tags, it seems like there should be a better way that involved only listing the ones you DON'T want. As to regex, I suppose that's the ideal way barring a native function... THANKS! Link to comment https://forums.phpfreaks.com/topic/231791-stripping-tags/#findComment-1192673 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.