parsing HTML with domDocument and DOMXPath
我将这段代码放入$ html变量中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | ... ... <table id="tbvalue" class="table_main"> <tr align="center"> <td> <img src="operation.bmp" border="0" alt="image" width="250" height="60" /> <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> ... ... |
注意:我需要第一次出现在
我正在尝试这样做:
1 2 3 4 5 6 7 8 9 10 11 | $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"); |
但是此查询不了解表
如何在
像这样做:
1 2 3 4 | <?php $xpath = new DOMXPath($dom); $spans = $xpath->query('//table[@id="tbvalue"]//img[1]'); echo $spans->item(0)->getAttribute("src"); |
您可以在此处找到更多有用的信息。