如何从html代码中删除所有样式类,从字符串中删除选择性html标记


<p>
<!--StartFragment-->
<ul style="box-sizing: border-box; margin: 0px 0px 10px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; color: rgb(65, 65, 65); font-family: 'Segoe UI', 'Helvetica Neue', 'Arial', 'sans-serif'; font-size: 17px;">
<li style="box-sizing: border-box; margin-top: 7px;">Fast Performance:&nbsp;Scripts written in PHP usually execute faster than those written in other scripting languages like ASP.NET or JSP.</li>
<li style="box-sizing: border-box; margin-top: 7px;">Vast Community:&nbsp;Since PHP is supported by the worldwide community, finding help or documentation for PHP online is extremely easy.</li>   
</ul>
<!--EndFragment-->  
<span id="sceditor-start-marker" class="sceditor-selection sceditor-ignore" style="display: none; line-height: 0;"> </span>
<span id="sceditor-end-marker" class="sceditor-selection sceditor-ignore" style="display: none; line-height: 0;"> </span><br>
</p>

如何从html代码中删除所有样式类,并从字符串中删除选择性html标记就像我不想删除<p>,<a>,<b>

$output = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $output);
$output = preg_replace('/(<[^>]+) class=".*?"/i', '$1', $output);
$answer = preg_replace('/(<[^>]+) id=".*?"/i', '$1', $output);

我找到了这种删除样式和类的方法,但它似乎很糟糕

要删除样式,请使用此regex-$output = preg_replace('/(<[^>]+) style=("|').*?("|')/i','$1',$output);

要删除类,请使用此regex-$output = preg_replace('/(<[^>]+) class=("|').*?("|')/i','$1',$output);

删除除白名单之外的所有标签-$output = strip_tags($output, '<p><a><b><i><table><tr><td><ul><ol><li><hr><h1><h2><h3><pre><caption><br><span><img>')

另请参阅此处从HTML标签中删除样式属性

最新更新