用链接标签替换 "some words"的 php 正则表达式,但应该排除链接标签内的 "some words"

标签 php regex

我将 html 内容存储在数据库表中。在该 html 内容中,我想用链接标记替换“SOME WORDS”。但是如果“SOME WORDS”已经在链接标签中,我应该忽略它们..

例如
内容

<p>Lorem ipsum dolor SOME WORDS, consectetur adipiscing elit. <a href="http://example.com">SOME WORDS</a> elementum pharetra velit at cursus. Quisque blandit, nibh at eleifend ullamcorper</p>

输出应该是

<p>Lorem ipsum dolor <a href="http://someurl">SOME WORDS</a>, consectetur adipiscing elit. <a href="http://example.com">SOME WORDS</a> elementum pharetra velit at cursus. Quisque blandit, nibh at eleifend ullamcorper</p>

如您所见,替换时应排除现有链接文本。

非常感谢一些进入正确轨道的指导。

最佳答案

这是使用 DOMDocument 而不是正则表达式来解决它的方法:

$contents = <<<EOS
<p>Lorem ipsum dolor SOME WORDS, consectetur adipiscing elit. <a href="http://example.com">SOME WORDS</a> elementum pharetra velit at cursus. Quisque blandit, nibh at eleifend ullamcorper</p>
EOS;

$doc = new DOMDocument;
libxml_use_internal_errors(true);
$doc->loadHTML($contents);
libxml_clear_errors();

$xp = new DOMXPath($doc);

// find all text nodes
foreach ($xp->query('//text()') as $node) {
        // make sure it's not inside an anchor
        if ($node->parentNode->nodeName !== 'a') {
                $node->nodeValue = str_replace(
                    'SOME WORDS', 
                    'SOME OTHER WORDS', 
                    $node->nodeValue
                );
        }
}
// DOMDocument creates a full document and puts your fragment inside a body tag
// So we enumerate the children and save their HTML representation
$body = $doc->getElementsByTagName('body')->item(0);
foreach ($body->childNodes as $node) {
        echo $doc->saveHTML($node);
}

关于用链接标签替换 "some words"的 php 正则表达式,但应该排除链接标签内的 "some words",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13889972/

相关文章:

php - 是否可以在 WAMP 中使用 PHP HttpRequest?

php - 查询两个单独的表,在第二个查询中传递结果,而如果第二个查询返回 null,则仅保留第一个查询的结果

php - 如何使用 php 更新 MySQL 中的一行?

javascript - 如何在javascript中分割断线?

php - 检查字符串是否包含 "\"

php - 为什么我的页面上有 �

javascript - 正则表达式匹配 3 个数字后的数字

python - 正则表达式:我的逻辑有缺陷

Javascript 正则表达式匹配具有非混合 unicode 字符范围的字符串

php - 教义 - [语义错误] - 错误 : Class has no field or association