我有这个代码
function innerHTML($element)
{
$innerHTML = '';
$children = $element -> childNodes;
foreach ((array)$children as $child)
{
$tmp_dom = new DOMDocument();
$tmp_dom -> appendChild($tmp_dom -> importNode($child, true));
$innerHTML .= trim($tmp_dom -> saveHTML());
}
return $innerHTML;
}
function DOMGet($html, $class, $num = 0, $type = "class")
{
$dom = new DOMDocument('1.0', 'utf-8');
@$dom -> loadHTML($html);
$xpath = new DOMXPath($dom);
$q = '//div[@'.$type.'="'.$class.'"]';
if(is_numeric($num))
{
$arr = $xpath -> query($q);
$arr = $arr -> item($num);
return innerHTML($arr);
}
else if(strtoupper($num) == "ALL")
{
$arr = $xpath -> query($q);
$abs = array();
foreach($arr as $el) $abs[] = innerHTML($el);
return $abs;
}
return false;
}
但它根本不起作用,有时当我尝试调用标签时(例如class =“test”)它没有显示任何内容!有人可以修复代码吗?
答案 0 :(得分:0)
你不能将DOMNodeList
强制转换为数组,它显然会返回一个空数组。
答案 1 :(得分:0)
这是你的m_INNER方法搞砸了你。你可以将它剥离到我的例子中,它会完美地运作。
function m_INNER($element)
{
$innerHTML = '';
$children = $element->childNodes;
foreach ($children as $child)
{
$innerHTML .= $child->ownerDocument->saveXML( $child );
}
return $innerHTML;
}
function DOMGet($html, $class, $num = 0, $type = "class")
{
$dom = new DOMDocument('1.0', 'utf-8');
@$dom -> loadHTML($html); // You don't need the @ declaration here.
$xpath = new DOMXPath($dom);
$q = '//div[@'.$type.'="'.$class.'"]';
if(is_numeric($num))
{
$arr = $xpath -> query($q);
$arr = $arr -> item($num);
return m_INNER($arr);
}
else if(strtoupper($num) == "ALL")
{
$arr = $xpath -> query($q);
$abs = array();
foreach($arr as $el) $abs[] = m_INNER($el);
return $abs;
}
return false;
}
$html = '<html><body><div class="apa">Test</div></body></html>';
print_r(DOMGet($html, "apa"));
有效
干杯