开发者社区> 问答> 正文

有谁知道如何通过url获取title吗

已解决

比如:url为www.baidu.com,返回的title为百度

展开
收起
潇莎 2016-02-19 08:52:54 3684 0
1 条回答
写回答
取消 提交回答
  • 旺旺:nectar2。
    采纳回答
    <?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.baidu.com/");
    
    ?>

    请参考:http://stackoverflow.com/questions/4348912/get-title-of-website-via-link


    以上php例子,运行结果如下图:

    01

    2019-07-17 18:29:37
    赞同 1 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载