【发布时间】:2015-02-28 23:04:18
【问题描述】:
我正在将此代码放入 $html 变量中:
...
...
<table id="tbvalue" class="table_main">
<tr align="center">
<td>
<div style='background-color:#534522;' ><img src="operation.bmp" border="0" alt="image" width="250" height="60" /></div>
<br />
</td>
</tr>
<tr align="center">
<td class="other">
more text
</td>
</tr>
<tr align="center">
<td>
<input name="name" type="text" id="label" tabindex="1"/>
</td>
</tr>
<tr>
<td>
<span id="lblErrCap" class="errfont"></span>
</td>
</tr>
</table>
...
...
注意:我需要 <img> 的第一次出现在 table id="tbvalue" 内部
我试图这样做:
$dom = new domDocument;
/*** load the html into the object ***/
@$dom->loadHTML($html); // the @ is to silence errors and misconfigures of HTML
/*** discard white space ***/
$dom->preserveWhiteSpace = false;
$xpath = new DOMXPath($dom);
$spans = $xpath->query('//img');
echo $spans->item(0)->getAttribute("src");
但是这个查询不知道 table id="tbvalue" 表,只会获取第一个 <img>。
在 table id="tbvalue" 中获取第一个 img 的方法是什么?
【问题讨论】:
标签: php html dom domdocument domxpath