returns this:

<p>This should be bold</p>
<p>This is awesome</p>
This should be bold

consequently I gotten rid of my formatting via <strong> and perhaps <em> in the future.

I want a way to blacklist rather than whitelist something like:

echo blacklist_tags($html,"<img>");

returning:

<p><strong>This should be bold<strong></p>
<p>This is awesome</p>
<strong>This should be bold<strong>

Is there any way to do this?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

If you only wish to remove the <img> tags, you can use DOMDocument instead of strip_tags().

$dom = new DOMDocument();
$dom->loadHTML($your_html_string);

// Find all the <img> tags
$imgs = $dom->getElementsByTagName("img");

// And remove them
$imgs_remove = array();
foreach ($imgs as $img) {
  $imgs_remove[] = $img;
}

foreach ($imgs_remove as $i) {
  $i->parentNode->removeChild($i);
}
$output = $dom->saveHTML();

Solution 2

You can only do this by writing a custom function. Strip_tags() is considered more secure though, because you might forget to blacklist some tags…

PS: Some example functions can be found in the comments on php.net’s strip_tags() page

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply