Search code examples
phpregexstrip-tags

remove hidden (display:none) html tags in php


I need a PHP regex that will remove (with display:none in their style) from a html code sequence. I tried exploding it and working it out but it turns out to be a mess. For example the html code below

 <span>
<span style="display: inline">69</span>
<span style="display:none">113</span>
<span></span>
<span class="" style="">.</span>
<span style="display: inline">86</span>
<span style="display:none">59</span>
<span></span>
.206
<span></span>
.143
</span>

should turn out like this:

<span>
<span style="display: inline">69</span>
<span></span>
<span class="" style="">.</span>
<span style="display: inline">86</span>
<span></span>
.206
<span></span>
.143
</span>

Thanks.


Solution

  • HTML is not a regular language and therefor cannot be parsed by regular expressions.

    You can use this though and then use what it returns with php:

    http://api.jquery.com/remove/

    $("span[style='display:none']").remove();
    

    If you don't want to do that, you can use dom and the removeChild function: http://php.net/manual/en/domnode.removechild.php