PHP/DOM方法更改不在<;中的图像路径;预处理>;或<;代码>;标签,不带JavaScript
任务:PHP/DOM方法更改不在<;中的图像路径;预处理>;或<;代码>;标签,不带JavaScript,php,image,function,dom,path,Php,Image,Function,Dom,Path,任务: <?php function changeImagePaths ($content) { // content here comes as string via some other functon. $dom = new DOMDocument; $dom->loadHTML($content); $nodes = $dom->getElementsByTagName('[self::img][not(ancestor::pre) and no
<?php
function changeImagePaths ($content) { // content here comes as string via some other functon.
$dom = new DOMDocument;
$dom->loadHTML($content);
$nodes = $dom->getElementsByTagName('[self::img][not(ancestor::pre) and not(ancestor::code)]');
foreach( $nodes as $node ) {
// What is the correct way to retrive all image-paths
// that are not wrapped within <pre> or <code> tags and how to use them in the code?
$node->nodeValue = str_replace('desktop-img', DEVICE, $node->nodeValue);
}
$content = $dom->saveHTML();
return $content;
}
?>
设备
,我已经使用其他函数定义了该值但不应替换
<div class="wrapper">
<img src="img/demo/desktop-img/sample1.jpg" width="200" height="200" alt="image">
<img src="img/demo/desktop-img/sample2.jpg" width="200" height="200" alt="image">
<pre>
<img src="img/demo/desktop-img/sample1.jpg" width="200" height="200" alt="image">
<img src="img/demo/desktop-img/sample2.jpg" width="200" height="200" alt="image">
</pre>
<code>
<img src="img/demo/desktop-img/sample1.jpg" width="200" height="200" alt="image">
<img src="img/demo/desktop-img/sample2.jpg" width="200" height="200" alt="image">
</code>
</div>
$xpath = new DOMXpath($dom);
foreach($xpath->query('//img[not(ancestor::pre) and not(ancestor::code)]') as $img){
$img->setAttribute('src', 'foo');
}
我尝试了以下操作,但它正在替换
<div class="wrapper">
<img src="img/demo/desktop-img/sample1.jpg" width="200" height="200" alt="image">
<img src="img/demo/desktop-img/sample2.jpg" width="200" height="200" alt="image">
<pre>
<img src="img/demo/desktop-img/sample1.jpg" width="200" height="200" alt="image">
<img src="img/demo/desktop-img/sample2.jpg" width="200" height="200" alt="image">
</pre>
<code>
<img src="img/demo/desktop-img/sample1.jpg" width="200" height="200" alt="image">
<img src="img/demo/desktop-img/sample2.jpg" width="200" height="200" alt="image">
</code>
</div>
$xpath = new DOMXpath($dom);
foreach($xpath->query('//img[not(ancestor::pre) and not(ancestor::code)]') as $img){
$img->setAttribute('src', 'foo');
}
挑战:
<?php
function changeImagePaths ($content) { // content here comes as string via some other functon.
$dom = new DOMDocument;
$dom->loadHTML($content);
$nodes = $dom->getElementsByTagName('[self::img][not(ancestor::pre) and not(ancestor::code)]');
foreach( $nodes as $node ) {
// What is the correct way to retrive all image-paths
// that are not wrapped within <pre> or <code> tags and how to use them in the code?
$node->nodeValue = str_replace('desktop-img', DEVICE, $node->nodeValue);
}
$content = $dom->saveHTML();
return $content;
}
?>
我认为使用DOM方法应该是可行的,但我无法找出正确的语法
我对编程非常陌生,因此仍在学习过程中。请温柔地用代码示例说明您的答案,以便于理解
我的问题:
DOM方法的正确语法应该是什么
使用DOM是正确的方法吗
使用DOM方法是否会遇到任何挑战/性能问题
有没有其他更好的方法不用JavaScript就可以做到这一点
这是一个快速的工作,所以它可能看起来不好,但希望你得到的想法
其思想是递归地遍历DOM树,跳过每个
和
,并更改遇到的所有
这很简单,但您可能会注意到,由于您将其视为HTML,DOM会自动添加一些标记来“实现”它,并以一种(IMO)非常奇怪的方式对其进行格式化。Xpath是您的朋友:
srt_替换应为str_replace@allen213谢谢更新并更正。想法:从根元素开始,递归地遍历子节点,但在
的情况下,跳过。@路人,请将想法转换为代码语法。正如我提到的,我对编程非常陌生,如果没有示例,我将很难理解。这不是工作原理。如果你需要有人为你写代码,那么你应该考虑雇用某人。你能修改你的答案的格式吗?所有这些线路都不是必需的