How to remove redundant
tags from HTML code using PHP?

前端 未结 5 1593
孤街浪徒
孤街浪徒 2021-01-14 00:52

I\'m parsing some messy HTML code with PHP in which there are some redundant
tags and I would like to clean them up a bit. For instance:


&
相关标签:
5条回答
  • 2021-01-14 01:02

    this will replace all breaks ... even if they're in uppercase:

    preg_replace('/<br[^>]*>/i', '', $string);
    
    0 讨论(0)
  • 2021-01-14 01:06

    Use str_replace, its much better for simple replacement, and you can also pass an array instead of a single search value.

    $newcode = str_replace("<br>", "", $messycode);
    
    0 讨论(0)
  • 2021-01-14 01:09

    Here is something you can use. The first line finds whenever there is 2 or more <br> tags (with whitespace between and different types) and replace them with wellformated <br /><br />.

    I also included the second line to clean up the rest of the <br> tags if you want that too.

    function clean($txt)
    {
        $txt=preg_replace("{(<br[\\s]*(>|\/>)\s*){2,}}i", "<br /><br />", $txt);
        $txt=preg_replace("{(<br[\\s]*(>|\/>)\s*)}i", "<br />", $txt);
        return $txt;
    }
    
    0 讨论(0)
  • 2021-01-14 01:12

    Try with:

    preg_replace('/<br\s*\/?>/', '', $inputString);
    
    0 讨论(0)
  • 2021-01-14 01:25

    This should work, using minimum specifier:

    preg_replace('/(<br[\s]?[\/]?>[\s]*){3,}/', '<br /><br />', $multibreaks);
    

    Should match appalling <br><br /><br/><br> constructions too.

    0 讨论(0)
提交回复
热议问题