AzyureDHikari Posted March 4, 2014 Share Posted March 4, 2014 Hi everyone, I need help with a coding. This is the HTML <span class="remove">no tag</span> <span class="do_not_touch_me">have tag</span> <h2 class="remove">no tag</h2> <h3 class="remove" id="this">no tag</h3> <h4 id="that" class="remove">no tag</h4> <h2 class="do_not_touch_me2">have tag</h2> <h3 class="do_not_touch_me3" id="this">have tag</h3> <h4 id="that" class="do_not_touch_me4">have tag</h4> I want to strip any tag that have ' class="remove" ' parameter in them. Does not matter if there's other parameter. As long ' class="remove" ' is there, the tag gets stripped, but not removing the content. This is what it's supposed to return: no tag <span class="do_not_touch_me">have tag</span> no tag no tag no tag <h2 class="do_not_touch_me2">have tag</h2> <h3 class="do_not_touch_me3" id="this">have tag</h3> <h4 id="that" class="do_not_touch_me4">have tag</h4> I've already managed to come out with the function below. All I need is to adjust the preg_replace parameter, but luck is not on my side. Already tried to google for it, but can't seem to find anything that does the job. Any help is much appreciated. function remove_unwanted_tags_while_saving($content) { $tag_to_remove = array("span","div","h2","h3"); foreach ($tag_to_remove as $tag) { $content= preg_replace("/<\/?" . $tag . "(.|\s)*?>/",'',$content); } return $content; } Link to comment https://forums.phpfreaks.com/topic/286690-need-help-strip-any-tags-with-a-specific-class-but-not-the-content/ Share on other sites More sharing options...
AzyureDHikari Posted March 4, 2014 Author Share Posted March 4, 2014 SOLVE. Code to share if anyone needed. $content = '<span class="remove">no tag</span><br><span class="do_not_touch_me">have tag</span><br><br><h2 class="remove">no tag</h2><br><h3 class="remove" id="this">no tag</h3><br><h4 id="that" class="remove">no tag</h4><br><br><br><h2 class="do_not_touch_me2">have tag</h2><br><h3 class="do_not_touch_me3" id="this">have tag</h3><br><h4 id="that" class="do_not_touch_me4">have tag</h4>'; function remove_unwanted_tags_while_saving($text) { $tags = array('span','h2','h3','h4'); foreach ($tags as $tag) { if(preg_match_all('/<'.$tag.'[^>]+class="remove"[^>]*>(.*)<\/'.$tag.'>/iU', $text, $found)) { $text = str_replace($found[0],$found[1],$text); } } return $text; } echo remove_unwanted_tags_while_saving($content); Link to comment https://forums.phpfreaks.com/topic/286690-need-help-strip-any-tags-with-a-specific-class-but-not-the-content/#findComment-1471414 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.