PHP DOMDocument GetElementsByTagName 未找到元素
我有一个包含大量元标记的 HTML 页面,我想解析它们以找到某些元标记。这是我正在使用的代码,但它没有获取任何标签。
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadHtml($contents);
$metaChildren = $dom->getElementsByTagName('meta');
var_dump($metaChildren);
这是我正在使用的 HTML 片段(我用大括号替换了箭头):
[meta name="GZPlatform" content=" pc"]
[meta name="GZFeatured" content=" Gone Gold"]
[meta name="GZHeadline" content=" pc"]
[meta name="GZP_ID" content=" 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您确定标签不匹配吗?
var_dump
的输出是什么?使用var_dump($metaChildren->length)
时会得到什么值?您的代码似乎在这里工作:给出输出:
Are you sure the tags aren't being matched? What is the output of
var_dump
? What value do you get when you usevar_dump($metaChildren->length)
? Your code seems to work here:Gives output:
我的猜测是 HTML 无效,并且
$dom->loadHtml
调用失败。我相信该调用会返回 true|false。所以也许是这样的:My guess would be that the HTML is not valid and that the
$dom->loadHtml
call is failing. I believe that call returns true|false. So maybe something like this:解析器是否希望您关闭元标记?
或者
COuld it be that the parser expects you to close the meta tags?
or