php - 使用 XPATH 删除 <p><strong><br/> </strong></p>

标签 php regex xpath domdocument

我使用 xpath 删除 <p>&nbsp;</p>

    $nodeList = $xpath->query("//p[text()=\"\xC2\xA0\"]"); # &nbsp;
    foreach($nodeList as $node) 
    {
        $node->parentNode->removeChild($node);
    }

但它不会删除这个,

<p><strong><br /> &nbsp;</strong></p>

还是这种,

<p><strong>&nbsp;</strong></p>

我怎样才能删除它们?

或者也许是我应该使用的正则表达式?

最佳答案

试试

$nodeList = $xpath->query("//p[normalize-space(.)=\"\xC2\xA0\"]"); # &nbsp;
foreach($nodeList as $node) 
{
    $node->parentNode->removeChild($node);
}

引用 from the docs

The normalize-space function returns the argument string with whitespace normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space.

关于php - 使用 XPATH 删除 <p><strong><br/> </strong></p>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7856414/

相关文章:

php - WordPress - 覆盖短代码

c - C语言正则表达式匹配字符串(忽略大小写)

xml - 在 XPath 中使用//不明确

python - pandas:列格式问题导致合并问题

php - 使用DOMDocument和DOMXPath类从网站中检索类别

xpath:如何选择第 n 个选项?

php - 在 zend db 中检索最后插入的 user_id

php - 在 hostgator 托管的 Web 服务器中安装 python 模块

PHPMailer:超链接出现在 outlook 中的括号中

javascript - 包含前后空格的三个字母和两个数字的 RegEx 字符串