0

I am trying to remove an html tag with a specific "for" attribute but then also remove everything in between. I a have only been able to remove the tage itself but not what is in between even though there are some html tags in there as well. Below is the code that i am using:

This is the string i am working with:

$content = <<<EOD <label for="cl-gallery-el-slide4-elm_527" data-interactive="true" class="cl-gallery-el-nav-arrow-wrapper cl-gallery-el-nav-arrow-wrapper-prev">
    <span class="cl-gallery-el-nav-arrow cl-gallery-el-nav-arrow-prev" data-interactive="true"></span>
</label>
EOD;

Below is the code for php

function strip_single_tag($str,$tag){

$str=preg_replace('/<'.$tag.'[^>]*>/i', '', $str);

$str=preg_replace('/<\/'.$tag.'>/i', '', $str);

return $str;
}   

$content = strip_single_tag($content,'label for="cl-gallery-el-slide4-elm_527"');
  • 2
    Any particular reason you're not using [DOMDocument](http://be2.php.net/manual/en/class.domdocument.php) ... or were you secretly hoping someone would post a link to [this answer](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) ? – CD001 Jun 26 '18 at 14:22
  • And where exactly does your HTML code contain ``, which is what you are trying to replace with your second preg_replace call here …? – CBroe Jun 26 '18 at 14:35
  • And even if you did not mess the above mentioned thing up - you would be replacing the starting and ending _tag_ of that label element only, how does that cover “in between” …? The span element would “survive” this procedure. – CBroe Jun 26 '18 at 14:37
  • correct, i am trying to remove the span that is in between as well – Gerald Germain Jun 26 '18 at 14:38
  • Well right now you are only cutting off a little bit at the beginning, and a little bit at the end (assuming above error was fixed) - of course it doesn’t work that way, you would need to match the whole thing in one go, from start to finish. – CBroe Jun 26 '18 at 14:42
  • so no way to take off everything in between? – Gerald Germain Jun 26 '18 at 14:46

0 Answers0