如何使用html简单dom获取Content-type?

发布于 2024-08-20 15:56:21 字数 73 浏览 14 评论 0 原文

我尝试 find('meta[http-equiv="Content-type"]') 但未能检索该信息。

I tried find('meta[http-equiv="Content-type"]') but it failed to retrieve that information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梅窗月明清似水 2024-08-27 15:56:21

SimpleHTMLDom 不在选择器中使用带引号的字符串文字。它只是elem[attr=value]。并且 value 的比较似乎区分大小写(可能有办法使其不区分大小写,但我不知道)*

例如

require 'simple_html_dom.php';
$html = file_get_html('http://www.google.com/');
// most likely one one element but foreach doesn't hurt
foreach( $html->find('meta[http-equiv=content-type]') as $ct ) { 
  echo $ct->content, "\n";
}

打印 text/html;字符集=ISO-8859-1

*编辑:是的,有一种方法可以执行不区分大小写的匹配,使用 *= 而不是 =

find('meta[http-equiv*=content-type]')

edit2: btw that http-equiv*=content -type thingy 也会匹配 (它只测试字符串是否位于属性值中的某个位置) 。但这是我能找到的唯一不区分大小写的函数/运算符。我想在这种情况下你可以接受它;-)
编辑3:它使用 preg_match('.../i') 并且模式/选择器直接传递给该函数。因此,您可以执行类似 http-equiv*=^content-type$ 的操作来匹配 http-equiv="Content-type" 但不能http-equiv="xyzContent-typeabc"。但我不知道这是否是一个有保证的功能。

SimpleHTMLDom doesn't use quoted string literals in the selector. It's just elem[attr=value]. And the comparison of value seems to be case-sensitive (there may be a way to make it case-insensitive, but that I don't know)*

E.g.

require 'simple_html_dom.php';
$html = file_get_html('http://www.google.com/');
// most likely one one element but foreach doesn't hurt
foreach( $html->find('meta[http-equiv=content-type]') as $ct ) { 
  echo $ct->content, "\n";
}

prints text/html; charset=ISO-8859-1.

*edit: yes, there is a way to perform a case-insensitive match, use *= instead of =

find('meta[http-equiv*=content-type]')

edit2: btw that http-equiv*=content-type thingy would also match <meta http-equiv="haha-no-content-types"... (it only tests if the string is somewhere in the attribute's value). But it's the only case-insensitive function/operator I could find. I guess you can live with it in this case ;-)
edit 3: It uses preg_match('.../i') and the pattern/selector is directly passed to that function. Therefore you could do something like http-equiv*=^content-type$ to match http-equiv="Content-type" but not http-equiv="xyzContent-typeabc". But I don't know if this is a warranted feature.

浮生面具三千个 2024-08-27 15:56:21

Content-Type 通常是 http 响应标头的一部分,而不是正文中。你从哪里得到xml文档?

The Content-Type is typically part of the http-response headers - not in the body. Where did you get the xml document from?

嘴硬脾气大 2024-08-27 15:56:21

如果 content-type 的书写方式不同,我会在 $this->find('meta'); 上进行 foreach - 我认为在这种情况下,浏览器不区分大小写,而 php 可能会。

I would go foreach on $this->find('meta'); in case of differently written content-type - I think that browsers aren't in this case case sensitive, while php might be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文