我有一个带有test.htm名称的html文件,其代码如下:
<div></div>
<div>
<div class="body">
<div>
<a href="login.php"><img src="./lklk_files/81755_236.jpg"></a>
</div>
<div class="wrapper"></div>
</div>
<div class="photo-holder"></div>
</div>
我想仅从具有“body”属性值的class属性的DIV中提取img src值 我使用下面的PHP代码:
<?php
$f = "test.htm";
$doc = new DOMDocument();
$doc->loadHTMLFile($f);
$xpath = new DOMXPath($doc);
$nodeList = $xpath->query("//div");
foreach($nodeList as $node)
if($node->getAttribute('class') == "body"){
$s = $node->nodeValue;
$doc2 = new DOMDocument();
$doc2->loadHTML($s);
$xpath2 = new DOMXPath($doc2);
$img = $xpath2->query("//img");
foreach($img as $g)
echo $g->attributes->getNamedItem('src')->nodeValue;
}
?>
但是当它运行时,说你的$ s是一个空字符串。我的代码在哪里出错了?
答案 0 :(得分:0)
你能试试这个,$domxpath->evaluate("string(//img/@src)");
$htmlString= '<div></div>
<div>
<div class="body">
<div>
<a href="login.php"><img src="./lklk_files/81755_236.jpg"></a>
</div>
<div class="wrapper"></div>
</div>
<div class="photo-holder"></div>
</div>';
$dom = new DOMDocument();
$dom->loadHTML($htmlString);
$domxpath = new DOMXPath($dom);
echo $src = $domxpath->evaluate("string(//img/@src)");
答案 1 :(得分:0)
这样做......
<?php
$html='<div></div>
<div>
<div class="body">
<div>
<a href="login.php"><img src="./lklk_files/81755_236.jpg"></a>
</div>
<div class="wrapper"></div>
</div>
<div class="photo-holder"></div>
</div>';
$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('div') as $tag) {
if($tag->getAttribute('class')=='body')
{
foreach($tag->getElementsByTagName('img') as $imgtag)
{
echo $imgtag->getAttribute('src');
exit;
}
}
}
输出:
./lklk_files/81755_236.jpg