According to the post here, the code below can remove the HTML tag, such as <div>
. But I found that the end tag </div>
still remain in the string.
$content = "<div id=\"header\">this is something with an <img src=\"test.png\"/> in it.</div>";
$content = preg_replace("/<div[^>]+\>/i", "", $content);
echo $content;
I have tried something below, but still not work, how can I fix this issue?
$content = preg_replace("/<\/div[^>]+\>/i", "", $content);
$content = preg_replace("/<(/)div[^>]+\>/i", "", $content);
Thanks
The end tag doesn't have anything between the div and the >
, so instead try something like:
$content = preg_replace("/<\/?div[^>]*\>/i", "", $content);
This will remove patterns of the form:
<div>
</div>
<div class=...>
change it to "/<[\/]*div[^>]*>/i"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With