我在字符串中有这样的表格:
<table>
<tr class='test'>
<td class='t' style='color: red'>
aaaaa
</td>
<td class='t'>
bbbbb
</td>
</tr>
<tr>
<td style='color: red'>
ccccc
</td>
<td class='t'>
ddddd
</td>
</tr>
</table>
现在我如何将所有td内容放在PHP中的数组中? 像这样:
array(
arg[0] => 'aaaaa',
arg[1] => 'bbbbb',
arg[2] => 'ccccc',
arg[3] => 'ddddd',
)
答案 0 :(得分:4)
尝试使用DOMDocument
解析HTML,并获取元素。
$result = array();
$dom = new DOMDocument;
$dom->loadHTML($table);
$xPath = new DOMXPath($dom);
$td = $xPath->query('//table/tr/td');
foreach($td as $val){
$result[] = $val->nodeValue;
}
print_r($result);