【问题标题】:curl page title卷曲页面标题
【发布时间】:2012-12-15 02:27:48
【问题描述】:

我正在使用以下代码从指定页面获取完整的html

$url = "http://www.google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close ($ch);

问题:如何修改此代码以返回<title>,而不是页面的完整html。 $result 存储结果。

【问题讨论】:

  • 如果您阅读 Google URL,您将获得页面的完整 HTML,并且可能还有很多 JavaScript 内容。 Ryan Naddy 的正则表达式在我看来是正确的。

标签: php html curl


【解决方案1】:

你可以使用正则表达式获取标题,我觉得这个正则表达式很有帮助:

function get_html_title($html){
    preg_match("/\<title.*\>(.*)\<\/title\>/isU", $html, $matches);
    return $matches[1];
}

【讨论】:

    【解决方案2】:

    你不能真的只得到标题,你可以得到整个文档然后剔除你需要的元素:我喜欢用Simple Html Dom Parser

    $html = file_get_html('http://www.google.com/');
    $title = $html->find('title');
    

    【讨论】:

    【解决方案3】:

    看看解析结果的内容

    使用正则表达式

    或Dom文档

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-08
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多