开发者社区 问答 正文

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

已解决

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

展开
收起
潇莎 2016-02-19 08:52:54 3767 分享
分享
版权
举报
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 评论

    评论

    全部评论 (0)

    登录后可评论
问答标签:
问答地址:
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等