PHP5’s DomDocument
$tableArray = array();
$dom = new DomDocument();
$dom->preserveWhiteSpace = false;
$dom->loadHTML($xmlstr);
// return DOMNodeList
$rows = $dom->getElementsByTagName('tr');
// loop through each row
// to work with DOMNode (nodeName, nodeType, parentNode, nodeValue, getAttribute
for ($rowIndex = 0, $length = $rows->length; $rowIndex < $length; $rowIndex++) {
// DOMElement
$currentRow = $rows->item($rowIndex);
// the same to while($currentRow->hasChildNodes())
// or foreach ($rows->item($rowIndex)->childNodes as $child) {
if (null !== $currentRow && $currentRow->hasChildNodes()) {
$cells = $currentRow->childNodes;
for ($cellIndex = 0, $length = $cells->length; $cellIndex < $length; $cellIndex++) {
$tableArray[$rowIndex][$cellIndex] = $cells->item($cellIndex)->nodeValue;
}
}
}
Đây là một cách dùng DOM để loop qua một table đơn giản
Phản hồi gần đây