str_replace within certain html tags only

你。 提交于 2019-12-02 07:23:22

Do not treat the HTML document as a mere string. Like you already noticed, tags/elements (and how they are nested) have meaning in an HTML page and thus, you want to use a tool that knows what to make of an HTML document. This would be DOM then:

Here is an example. First some HTML to work with

$html = <<< HTML
<body>
    <h1>Germany reached the semi finals!!!</h1>
    <h2>Germany reached the semi finals!!!</h2>
    <h3>Germany reached the semi finals!!!</h3>
    <h4>Germany reached the semi finals!!!</h4>
    <h5>Germany reached the semi finals!!!</h5>
    <p>Fans in Germany are totally excited over their team's 4:0 win today</p>
</body>
HTML;

And here is the actual code you would need to make Argentina happy

$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//*[self::h1 or self::h2 or self::p]');
foreach( $nodes as $node ) {
    $node->nodeValue = str_replace('Germany', 'Argentina', $node->nodeValue);
}
echo $dom->saveHTML();

Just add the tags you want to replace content in the XPath query call. An alternative to using XPath would be to use DOMDocument::getElementsByTagName, which you might know from JavaScript:

 $nodes = $dom->getElementsByTagName('h1');

In fact, if you know it from JavaScript, you might know a lot more of it, because DOM is actually a language agnostic API defined by the W3C and implemented in many languages. The advantage of XPath over getElementsByTagName is obviously that you can query multiple nodes in one go. The drawback is, you have to know XPath :)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!