可能重复:
Get data only from html table used preg_match_all in php
HTML:
<div class="table">
<dl>
<dt>ID:</dt>
<dd>632991</dd>
<dt>Type:</dt>
<dd>NEW</dd>
<dt>Body Type:</dt>
<dd>Compact</dd>
</dl>
</div>
在PHP中使用simple_html_dom获取此功能的最佳方法是什么:
PHP:
$option = array(
'id' => 632991,
'Type' => 'NEW',
'Body Type' => 'Compact'
);
答案 0 :(得分:1)
您可以使用XPath:
Getting DOM elements by classname
以下是Stackoverflow上的很多帖子。在这里使用搜索。
修改强>
<?php
$dom = new DOMDocument();
$dom->loadHTML('<div class="table">
<dl class="list">
<dt>ID:</dt>
<dd>632991</dd>
<dt>Type:</dt>
<dd>NEW</dd>
<dt>Body Type:</dt>
<dd>Compact</dd>
</dl>
</div>');
$nodes = $dom->getElementsByTagName('dl');
foreach ($nodes as $node) {
var_dump(getArray($node));
}
function getArray($node) {
$array = false;
if ($node->hasAttributes()) {
foreach ($node->attributes as $attr) {
$array[$attr->nodeName] = $attr->nodeValue;
}
}
if ($node->hasChildNodes()) {
if ($node->childNodes->length == 1) {
$array[$node->firstChild->nodeName] = $node->firstChild->nodeValue;
} else {
foreach ($node->childNodes as $childNode) {
if ($childNode->nodeType != XML_TEXT_NODE) {
$array[$childNode->nodeName][] = getArray($childNode);
}
}
}
}
return $array;
}
?>
函数getArray来自php.net