Jump to content

Stripping Tags


Arty Ziff

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.