str_replace within certain html tags only

前端 未结 1 1205
慢半拍i
慢半拍i 2021-01-24 18:20

I have an html page loaded into a PHP variable and am using str_replace to change certain words with other words. The only problem is that if one of these words appears in an im

相关标签:
1条回答
  • 2021-01-24 19:03

    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 :)

    0 讨论(0)
提交回复
热议问题