0

我有

<?php
    echo "<div id='first'>
           <div id='getimg'> 
              <img src = 'mybyk.jpg'/> 
           </div>
          </div>";
?>

我正在尝试更改img src使用 xpath。
喜欢 :-

<?php
$doc = new DomDocument();
$doc->load("image.php");
$x = new DomXPath($doc);     
$re = $x->query("/div/img[@src]");
$m = $re->item(0)->SetAttribute("src","MyCar.jpg");
$doc->save("image.php");
?>

在这里,我尝试使用 xpath 在 img 中查找图像 src,如果是的话。然后用我的新图像名称 setAttribute src 我
成功地完成了这个
,但是当它保存这一切之后,它在我的新image.php
xml 版本中包含顶行,例如:-
<?xml version="1.0"?>
我不想要这个。
我怎样才能删除这个。
以及它为什么来。
谢谢。

4

2 回答 2

0

How about this?

file_put_contents('image.php', $doc->saveXML($doc->documentElement));

saveXML generates a string instead of saving directly to a file, but it allows you to specify a child node to start saving at. By specifying documentElement (which is the outer div), we're no longer saving the entire document, and the <?xml line is not generated.

于 2013-06-02T05:15:32.360 回答
0

不是
/div/img[@src]


/div/div/img[@src]//div/img[@src]//div[@id='getimg']/img[@src]

于 2013-06-02T04:58:58.137 回答