PHP DOMDocument GetElementsByTagName不查找元素 [英] PHP DOMDocument GetElementsByTagName Not Finding Elements
本文介绍了PHP DOMDocument GetElementsByTagName不查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ dom = new DOMDocument();
$ dom-> preserveWhiteSpace = false;
$ dom-> loadHtml($ contents);
$ metaChildren = $ dom-> getElementsByTagName('meta');
var_dump($ metaChildren);
以下是我使用的HTML代码段(我用大括号替换了箭头):
[meta name =GZPlatformcontent =pc]
[meta name =GZFeaturedcontent = G $金钱]
[meta name =GZHeadlinecontent =pc]
[meta name =GZP_IDcontent =pc 21153]
任何想法?
解决方案
你确定标签不匹配? var_dump
的输出是什么?您使用 var_dump($ metaChildren-> length)
时会获得什么价值?你的代码似乎在这里工作:
<?
$ dom = new DOMDocument();
$ dom-> preserveWhiteSpace = false;
$ dom-> loadHtmlFile('test.html');
$ metaChildren = $ dom-> getElementsByTagName('meta'); ($ i = 0; $ i <$ metaChildren-> length; $ i ++){
$ el = $ metaChildren-> item($ i);
print $ el-> getAttribute('name')。 '='。 $ el-> getAttribute('content')。 \\\
;
}
?>
提供输出:
GZPlatform = pc
GZFeatured = Gone Gold
GZHeadline = pc
GZP_ID = pc 21153
I have an HTML page containing alot of meta tags and I want to parse them to find certain ones. Here is the code I am using, but it's not picking up any of the tags.
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadHtml($contents);
$metaChildren = $dom->getElementsByTagName('meta');
var_dump($metaChildren);
Here is a snippet of the HTML I am using (I replaced the arrow with a brace):
[meta name="GZPlatform" content=" pc"]
[meta name="GZFeatured" content=" Gone Gold"]
[meta name="GZHeadline" content=" pc"]
[meta name="GZP_ID" content=" pc 21153"]
Any Ideas?
解决方案
Are you sure the tags aren't being matched? What is the output of var_dump
? What value do you get when you use var_dump($metaChildren->length)
? Your code seems to work here:
<?
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadHtmlFile('test.html');
$metaChildren = $dom->getElementsByTagName('meta');
for ($i = 0; $i < $metaChildren->length; $i++) {
$el = $metaChildren->item($i);
print $el->getAttribute('name') . '=' . $el->getAttribute('content') . "\n";
}
?>
Gives output:
GZPlatform= pc
GZFeatured= Gone Gold
GZHeadline= pc
GZP_ID= pc 21153
这篇关于PHP DOMDocument GetElementsByTagName不查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文