使用该页面的网址从网页中提取标题

时间:2012-11-05 17:53:13

标签: php html

  

可能重复:
  How to extract img src, title and alt from html using php?

我想从Digg.com复制一些功能,当你发布一个新地址时,它会自动扫描网址并找到页面标题。

请告诉我们在php中是如何完成的......有没有其他管理系统可供您制作像digg这样的网站

3 个答案:

答案 0 :(得分:1)

您可以使用file_get_contents()从页面获取数据,然后使用preg_match()和正则表达式模式来获取<title></title>之间的数据

'/<title>(.*?)<\/title>'/

答案 1 :(得分:0)

您可以使用Ajax调用服务器来实现此目的,您可以在其中卷曲URL并发回所需的所有详细信息。您可能对标题,描述,关键字等感兴趣。

答案 2 :(得分:0)

function get_title($url) {
  $ch = curl_init();
  $titleName = '';
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $data = curl_exec($ch);
  curl_close($ch);


  // data will contain the whole page you are looking for
  // You need to parse it for the string like this <title>Google</title>
  // start = strrpos($data, '<title>');
  // end = strrpos($data, '</title>');
  // substr($data, $start + 6, $end); 6 - length of title
  return $titleName;
}

您需要实施更智能的解析方式,因为<title > Google < /title>无法找到。