php - 通过链接获取网站标题

标签 php function url hyperlink page-title

注意Google News每篇文章摘录的底部都有来源。

The Guardian - ABC News - Reuters - Bloomberg

我正在尝试模仿。

例如,提交 URL http://www.washingtontimes.com/news/2010/dec/3/debt-panel-fails-test-vote/ 我想返回 华盛顿时报

用 php 怎么可能?

最佳答案

我的答案正在扩展@AI W 使用页面标题的答案。下面是完成他所说的代码。

<?php

function get_title($url){
  $str = file_get_contents($url);
  if(strlen($str)>0){
    $str = trim(preg_replace('/\s+/', ' ', $str)); // supports line breaks inside <title>
    preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case
    return $title[1];
  }
}
//Example:
echo get_title("http://www.washingtontimes.com/");

?>

输出

Washington Times - Politics, Breaking News, US and World News

如您所见,这并不是 Google 所使用的,因此这让我相信他们获得了 URL 的主机名并将其与自己的列表相匹配。

http://www.washingtontimes.com/ => 华盛顿时报

关于php - 通过链接获取网站标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4348912/

相关文章:

php - 使用 .htaccess 重写 URL 以缩短链接

java - 如何在我的 RestService 中创建 URL?

php - 检索所有 morphedBy 关系

php - 如何将字符串中的两个字符视为一个字节?

php - 如何在 Magento 中重新加载 block ?

C++计算数组中正数/负数的数量

php - 在wordpress中下拉框选择后获取子类别

visual-studio-2010 - C 函数调用中的堆栈溢出 - MS Visual C++ 2010 Express

C++ : Function return String in Static Library give Error : expected '=' , ',' , ';' , 'asm' 或 '__attribute__' token 之前的 ':'

php - 如何在 url php 中返回 $_GET 变量的名称