用链接替换文本中的单词,但不替换图片中的单词

时间:2018-10-12 14:20:25

标签: php wordpress

我有条短信。 本文为图片。 我想用链接替换每个字的柏林。 现在我有了问题,它也位于柏林的图片的旧部分,并且也被替换了。

<?php
$content = "
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
<img class='bild' src='https://berlin.link/ alt='Bild von Berlin' width='300' height='200' />
Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet, vulputate, arcu.
";

$content = str_replace("Berlin", "<a href='//Berlin.test'>Berlin</a>", $content);

return $content;
?>

图片中的柏林是否有可能未被替换? 注意该代码只是一个示例。我通常从wordpress获得内容。 但这与它无关。我想。

2 个答案:

答案 0 :(得分:0)

尝试一下:

$content = "Berlin Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. <img class='bild' src='https://berlin.link/ alt='Bild von Berlin' width='300' height='200' /> Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede.";

$replaced = preg_replace('/<.*?>(*SKIP)(*FAIL)|Berlin/', "<a href='//Berlin.test'>Berlin</a>", $content);

var_dump(htmlentities($replaced));

因此,使用preg_replace,正则表达式的第一部分(在|符号之前)告诉它跳过<...>标记内的所有内容。之后|是您要替换的。第二个参数是您要替换的参数。

希望这会有所帮助!

信用证转到:https://stackoverflow.com/a/29973967/3884381(我以前做过这种事情,但不记得具体细节,所以必须查一下。)

答案 1 :(得分:0)

通常,解析HTML的最可靠方法是使用DOM解析器,而不是字符串操作。这相当复杂,但是在出现奇怪情况时也不会失败。

$content = "
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
<img class='bild' src='https://berlin.link/' alt='Bild von Berlin' width='300' height='200' />
Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet, vulputate, arcu.
Berlin
";

$search = "Berlin";
$replace_href = "//Berlin.test/";

$dom = new DomDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DomXPath($dom);
// here we search for every text node with the search term in it
$nodes = $xpath->query("//text()[contains(., \"$search\")]");
foreach ($nodes as $node) {
    // explode the string at the search term to get an array
    $parts = explode($search, $node->nodeValue);
    // treat the first one differently because it comes before any matches
    $part1 = $dom->createTextNode(array_shift($parts));
    $node->parentNode->insertBefore($part1, $node);
    // now go through the rest of the string parts
    foreach ($parts as $part) {
        // create the a element, set the attribute value and text content
        $anchor = $dom->createElement("a");
        $anchor->setAttribute("href", $replace_href);
        $anchor->appendChild($dom->createTextNode($search));
        // append it in place
        $node->parentNode->insertBefore($anchor, $node);
        // and then put the original text back
        $node->parentNode->insertBefore($dom->createTextNode($part), $node);
    }
    // get rid of the original text node
    $node->parentNode->removeChild($node);
}
echo $dom->saveHTML();

输出:

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
<img class="bild" src="https://berlin.link/" alt="Bild von Berlin" width="300" height="200">
Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet, vulputate, arcu.
<a href="//Berlin.test/">Berlin</a>
</p>