Jump to content

AzyureDHikari

New Members
  • Posts

    2
  • Joined

  • Last visited

AzyureDHikari's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. 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);
  2. 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; }
×
×
  • 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.