简体   繁体   中英

PHP DomXpath HTML Table Value By Text

i have a small issue regarding extracting td value by th value

here is a table example:

<table>
<tr><th>Name:</th><td>Will</td></tr>
<tr><th>Size:</th><td>41</td></tr>
<tr><th>Clock:</th><td>GMT</td></tr>
</table>

i Would like to get The "Will" value By Th value "Name:"

in Jquery i do :

$('th:contains(Name:)').next().text(); //Will Show Will
$('th:contains(Size:)').next().text(); //Will show 41

However would like to do it via PHP DOMDocument

$dom = new DOMDocument();
$html = $dom->loadHTMLFile("src/load.html");
$dom->preserveWhiteSpace = false; 
$selector = new DOMXPath($dom);

$name=$selector->query('//th[contains(.,"Name:")]')->?
$size=$selector->query('//th[contains(.,"Size:")]')->?

Thank you

//th[contains(.,"Name:")]/following-sibling::td[1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM