str_replace 仅在某些 html 标签内

发布于 2024-09-08 02:35:29 字数 601 浏览 4 评论 0原文

我有一个 html 页面加载到 PHP 变量中,并使用 str_replace 将某些单词更改为其他单词。唯一的问题是,如果其中一个单词出现在一段重要的代码中,那么整个代码就会崩溃。

有没有办法只将 str_replace 函数应用于某些 html 标签?特别是: p,h1,h2,h3,h4,h5

编辑:

重要的代码:

 $yay = str_ireplace($find, $replace , $html); 

提前欢呼并感谢任何答案。

编辑 - 进一步澄清:

$find 和 $replace 是包含要查找和替换的单词(分别)的数组。 $html 是包含所有 html 代码的字符串。

一个很好的例子就是如果我要查找并替换出现在例如域名中的单词。所以如果我想用“奶酪”代替“帽子”这个词。 这样的绝对路径的情况

任何出现像www.worldofhat.com/images/monkey.jpg 将替换为: www.worldofcheese.com/images/monkey.jpg

因此,如果替换只能发生在某些标签中,则可以避免这种情况。

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 important peice of code then the whole thing falls to bits.

Is there any way to only apply the str_replace function to certain html tags? Particularly: p,h1,h2,h3,h4,h5

EDIT:

The bit of code that matters:

 $yay = str_ireplace($find, $replace , $html); 

cheers and thanks in advance for any answers.

EDIT - FURTHER CLARIFICATION:

$find and $replace are arrays containing words to be found and replaced (respectively). $html is the string containing all the html code.

a good example of it falling to bits would be if I were to find and replace a word that occured in e.g. the domain name. So if I wanted to replace the word 'hat' with 'cheese'. Any occurance of an absolute path like

www.worldofhat.com/images/monkey.jpg
would be replaced with:
www.worldofcheese.com/images/monkey.jpg

So if the replacements could only occur in certain tags, this could be avoided.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静水深流 2024-09-15 02:35:29

不要将 HTML 文档视为单纯的字符串。正如您已经注意到的,标签/元素(以及它们的嵌套方式)在 HTML 页面中具有意义,因此,您需要使用一个知道如何构成 HTML 文档的工具。这将是 DOM 那么:

这是一个示例。首先是一些要使用的 HTML

$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;

下面是您需要让阿根廷满意的实际代码

$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();

只需添加您想要替换 XPath 查询调用中的内容的标签即可。使用 XPath 的替代方法是使用 DOMDocument::getElementsByTagName, 事实上,如果您通过JavaScript

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

了解了它,您可能会了解更多,因为 DOM 实际上是由 W3C 定义的与语言无关的 API,并以多种语言实现。 XPath 相对于 getElementsByTagName 的优势显然是可以一次性查询多个节点。缺点是,您必须了解 XPath :)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文