【问题标题】:How to get Content-type using html simple dom?如何使用 html simple dom 获取 Content-type?
【发布时间】:2011-01-13 21:29:10
【问题描述】:

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

【问题讨论】:

  • "simple dom" 表示 simplehtmldom 就像在 simplehtmldom.sourceforge.net 中一样?

标签: php content-type simple-html-dom


【解决方案1】:

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; charset=ISO-8859-1

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

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

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

【讨论】:

  • 谢谢,我会很开心的!
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 2014-08-22
      • 1970-01-01
      • 2013-07-22
      • 2019-10-22
      • 2012-08-12
      • 2012-04-08
      相关资源
      最近更新 更多