gpt4 book ai didi

php - 去除 PHP 中所有空的 HTML 标记对

转载 作者:太空宇宙 更新时间:2023-11-04 14:36:14 25 4
gpt4 key购买 nike

我正在寻找一种方法来去除所有空的 HTML 标记对,例如字符串中的 <strong></strong><p class="bold"></p>。虽然为此目的找到正则表达式相对容易,但我找不到可以可靠地与 PHP 的 preg_replace() 一起工作的正则表达式。这是我尝试过的功能之一(取自 https://stackoverflow.com/a/5573115/1784564 ):

function strip_empty_tags($text) {
// Match empty elements (attribute values may have angle brackets).
$re = '%
# Regex to match an empty HTML 4.01 Transitional element.
< # Opening tag opening "<" delimiter.
((?!iframe)\w+)\b # $1 Tag name.
(?: # Non-capture group for optional attribute(s).
\s+ # Attributes must be separated by whitespace.
[\w\-.:]+ # Attribute name is required for attr=value pair.
(?: # Non-capture group for optional attribute value.
\s*=\s* # Name and value separated by "=" and optional ws.
(?: # Non-capture group for attrib value alternatives.
"[^"]*" # Double quoted string.
| \'[^\']*\' # Single quoted string.
| [\w\-.:]+ # Non-quoted attrib value can be A-Z0-9-._:
) # End of attribute value alternatives.
)? # Attribute value is optional.
)* # Allow zero or more attribute=value pairs
\s* # Whitespace is allowed before closing delimiter.
> # Opening tag closing ">" delimiter.
\s* # Content is zero or more whitespace.
</\1\s*> # Element closing tag.
%x';
while (preg_match($re, $text)) {
// Recursively remove innermost empty elements.
$text = preg_replace($re, '', $text);
}

return $text;
}

这是我一直在测试的 HTML:

<strong class="a b">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.l<br class="a  b" />fd<br class="a  b" /><br class="a  b" /></strong><strong class="a b"></strong><strong class="a b"><br class="a  b" /></strong><strong class="a b"></strong><br class="a  b" /><strong class="a b"><br class="a  b" /><br class="a  b" /></strong>

到目前为止,我尝试过的所有方法(已经尝试了 4 个多小时)似乎都去除了一些标签,但不是所有标签,这让我抓狂。任何帮助将不胜感激。

最佳答案

需要一个 unicode regex因为示例“空”标签实际上是 not empty :

$re = '~<(\w+)[^>]*>[\p{Z}\p{C}]*</\1>~u';

\p{Z} ...任何类型的空格或不可见的分隔符
\p{C} ...不可见的控制字符和未使用的代码点

二手 u (PCRE_UTF8) modifier ; test at regex101


还包括 <br> , <br />作为空元素:

$re = '~<(\w+)[^>]*>(?>[\p{Z}\p{C}]|<br\b[^>]*>)*</\1>~ui';

test at regex 101


同时将标签与空间实体匹配

$re = '~<(\w+)[^>]*>(?>[\p{Z}\p{C}]|<br\b[^>]*>|&(?:(?:nb|thin|zwnb|e[nm])sp|zwnj|#xfeff|#xa0|#160|#65279);)*</\1>~iu'

test at regex101 ;根据您的需要进行修改。


使用 recursive regex (没有 while 循环)

$re = '~<(\w+)[^>]*>(?>[\p{Z}\p{C}]|<br\b[^>]*>|&(?:(?:nb|thin|zwnb|e[nm])sp|zwnj|#xfeff|#xa0|#160|#65279);|(?R))*</\1>~iu';

test at regex101

关于php - 去除 PHP 中所有空的 HTML 标记对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25748002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com