HTML如何获取这些值使用XPath
<table id='dg' border='0' class="Table">
<tr>
<td class='text'>id.</td>
<td class='text'>file</td>
<td class='text'>alt</td>
</tr>
<tr>
<td class='text'><input name='somename[]' type='hidden' value='1234'>
1</td>
<td class='text'><a href='#' onClick="javascript:openPopWindow('../../somefile.jpg');" ><img src='cms_thumb.php?imgsrc=somefile2.jpg' border='0' ></a></td>
<td class='text'><input type='text' name='title[]' value='Value 1'></td>
</tr>
<tr>
<td class='text'><input name='somename[]2' type='hidden' value='2345'>
2</td>
<td class='text'><a href='#' onClick="javascript:openPopWindow('../../somefile2.jpg');" ><img src='thumb.php?imgsrc=somefile2.jpg' border='0' ></a></td>
<td class='text'><input type='text' name='title[]' value='Value 2'></td>
</tr>
</table>
目的
需要得到IMG SRC文件名,并取得具有名称=标题[]
输入字段的值我该怎么办?
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
foreach ($dom->getElementsByTagName('tr') as $node) {
$img = $xpath->query('//img')->item(0);
$img = str_replace("\'","",$img->getAttribute('src'));
$img = str_replace("cms_thumb.php?imgsrc=","",$img);
echo $img.'<br>';
}
$ IMG只包含第一图像,而不是其他
+1 - 写得很好的问题:) –
'$ xpath-> query('// img') - > item(0)' - 好吧,你是_explicitly_只能得到第一个图像(索引为0的项目在匹配元素的列表中) – lanzz
而你的'$ xpath'基于整个Dom,不仅是第一个'td'节点。 – j0k