How to remove redundant
tags from HTML code using PHP?

前端 未结 5 1602
孤街浪徒
孤街浪徒 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:09

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

    .

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

    function clean($txt)
    {
        $txt=preg_replace("{(|\/>)\s*){2,}}i", "

    ", $txt); $txt=preg_replace("{(|\/>)\s*)}i", "
    ", $txt); return $txt; }

提交回复
热议问题