您现在的位置是:首页 > PHP学习

李清波 2020-11-12 PHP学习 1542 复制当前网址

PHP获取网页标题的3种实现方法代码实例

一、推荐方法 CURL获取

<?php
    $c = curl_init();
    $url = 'www.liqingbo.cn';
    curl_setopt($c, CURLOPT_URL, $url);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($c);
    curl_close($c);
    $pos = strpos($data,'utf-8');
    if($pos===false){
        $data = iconv("gbk","utf-8",$data);
    }
    preg_match("/<title>(.*)<\/title>/i",$data, $title);
    echo $title[1];
?>


二、使用file()函数

<?php
    $lines_array = file('https://www.liqingbo.cn/');
    $lines_string = implode('', $lines_array); 
    $pos = strpos($lines_string,'utf-8');
    if($pos===false){
        $lines_string = iconv("gbk","utf-8",$lines_string);
    }
    eregi("<title>(.*)</title>", $lines_string, $title);
    echo $title[1];
?>



三、使用file_get_contents

<?php
    $content = file_get_contents("https://www.liqingbo.cn/");
    $pos = strpos($content,'utf-8');
    if($pos===false){
        $content = iconv("gbk","utf-8",$content);
    }
    $postb = strpos($content,'<title>')+7;
    $poste = strpos($content,'</title>');
    $length = $poste-$postb;
    echo substr($content,$postb,$length);
?>


文章来源:https://www.liqingbo.com/blog-1737.html

评论