如何使用HTML DOM简单获取内容类型?
-
19-09-2019 - |
题
我试图find('meta[http-equiv="Content-type"]')
但未能检索该信息。
解决方案
SimpleHTMLDom不选择使用引用字符串文字。这只是elem[attr=value]
。和比较值似乎是区分大小写的(可能有办法让它不区分大小写的,但我不知道)*
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";
}
打印text/html; charset=ISO-8859-1
。
*编辑:是的,有执行不区分大小写匹配的方式,使用*=
代替=
find('meta[http-equiv*=content-type]')
EDIT2:顺便说一句该http-equiv*=content-type
啄也将匹配<meta http-equiv="haha-no-content-types"...
(它仅如果字符串是在某处属性值测试)。但它是唯一不区分大小写的功能/操作员,我能找到。我想你可以在这种情况下;-)忍受它点击
编辑3:它采用的preg_match(“... / I”)和图形/选择器被直接传递给该函数。因此,你的 的可以做类似http-equiv*=^content-type$
匹配http-equiv="Content-type"
但不http-equiv="xyzContent-typeabc"
。但我不知道这是否是一个保证的功能。
其他提示
在内容类型是通常在HTTP响应的报头部分 - 不能在体内。你从哪里得到的XML文档?
我会去foreach
上$this->find('meta');
在不同书面content-type
的情况下 - 我认为浏览器是不是在这种情况下大小写敏感的,而PHP可能是
不隶属于 StackOverflow