说我有下表:
<table>
<thead>
<tr>
<th>Country<th>
<th>Currency Name</th>
<th>Currency Code</th>
<tbody>
<tr>
<td>Austria</td>
<td>Euro</td>
<td>EUR</td>
</tr>
<tr>
<td>South Africa</td>
<td>South African Rand</td>
<td>ZAR</td>
</tr>
<tr>
<td>Greece</td>
<td>Euro</td>
<td>EUR</td>
</tr>
</tbody>
</table>
如何获取包含<tr>
的整个<td>ZAR</td>
和内容?使用简单的HTML DOM和PHP?
答案 0 :(得分:2)
$string = '...'; // your HTML string
$html = str_get_html($string);
foreach ($html->find('tr') as $tr) {
$flag = 0;
foreach ($tr->find('td') as $td) {
if ($tr->plaintext === 'ZAR') {
$flag = 1;
break;
}
}
if ($flag === 1) {
echo $tr."\n";
}
}
答案 1 :(得分:1)
简单是不够聪明直接做到的,但是对于大多数像样的库(phpquery?),你可以这样做:
$doc->find('tr:has(td:contains("ZAR"))')