从 html 标记中删除所有属性

php
2022-08-30 10:25:30

我有这个html代码:

<p style="padding:0px;">
  <strong style="padding:0;margin:0;">hello</strong>
</p>

如何从所有标签中删除属性?我希望它看起来像这样:

<p>
  <strong>hello</strong>
</p>

答案 1

改编自我对类似问题的回答

$text = '<p style="padding:0px;"><strong style="padding:0;margin:0;">hello</strong></p>';

echo preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/si",'<$1$2>', $text);

// <p><strong>hello</strong></p>

正则表达式细分:

/              # Start Pattern
 <             # Match '<' at beginning of tags
 (             # Start Capture Group $1 - Tag Name
  [a-z]        # Match 'a' through 'z'
  [a-z0-9]*    # Match 'a' through 'z' or '0' through '9' zero or more times
 )             # End Capture Group
 [^>]*?        # Match anything other than '>', Zero or More times, not-greedy (wont eat the /)
 (\/?)         # Capture Group $2 - '/' if it is there
 >             # Match '>'
/is            # End Pattern - Case Insensitive & Multi-line ability

添加一些引用,并使用替换文本,它应该去除标签名称后面的任何文本,直到标签结束或只是。<$1$2>/>>

请注意:这不一定适用于所有输入,正如Anti-HTML + RegExp会告诉你的那样。有一些回退,最明显的是最终会结束和其他一些损坏的问题......我建议将Zend_Filter_StripTags视为PHP中更完整的证明标签/属性过滤器<p style=">"><p>">


答案 2

以下是如何使用本机 DOM 执行此操作:

$dom = new DOMDocument;                 // init new DOMDocument
$dom->loadHTML($html);                  // load HTML into it
$xpath = new DOMXPath($dom);            // create a new XPath
$nodes = $xpath->query('//*[@style]');  // Find elements with a style attribute
foreach ($nodes as $node) {              // Iterate over found elements
    $node->removeAttribute('style');    // Remove style attribute
}
echo $dom->saveHTML();                  // output cleaned HTML

如果要从所有可能的标记中删除所有可能的属性,请执行

$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//@*');
foreach ($nodes as $node) {
    $node->parentNode->removeAttribute($node->nodeName);
}
echo $dom->saveHTML();

推荐