str_replace 仅在某些 html 标签内 [英] str_replace within certain html tags only

查看:33
本文介绍了str_replace 仅在某些 html 标签内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

重要的代码:

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

欢呼并提前感谢您提供任何答案.

cheers and thanks in advance for any answers.

编辑 - 进一步澄清:

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

$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将替换为:www.worldofcheese.com/images/monkey.jpg

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.

推荐答案

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

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:

这是一个例子.首先使用一些 HTML

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

只需在 XPath 查询调用中添加要替换内容的标签即可.使用 XPath 的替代方法是使用 DOMDocument::getElementsByTagName,您可能从 JavaScript 中了解到:

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');

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

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

这篇关于str_replace 仅在某些 html 标签内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆