我将 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/