How can I remove attributes from an html tag?

前端 未结 6 440
醉梦人生
醉梦人生 2020-11-29 09:52

How can I use php to strip all/any attributes from a tag, say a paragraph tag?

to

6条回答
  •  囚心锁ツ
    2020-11-29 10:03

    Although there are better ways, you could actually strip arguments from html tags with a regular expression:

    [^<]*)/i'; // match any start tag
    
        $chunks = preg_split($regEx, $htmlString, -1,  PREG_SPLIT_DELIM_CAPTURE);
        $chunkCount = count($chunks);
    
        $strippedString = '';
        for ($n = 1; $n < $chunkCount; $n++) {
            $strippedString .= $chunks[$n];
        }
    
        return $strippedString;
    }
    ?>
    

    The above could probably be written in less characters, but it does the job (quick and dirty).

提交回复
热议问题