时间:2020-08-20加入收藏
<?php
header("Content-Type:text/html;charset=utf-8");
$url = 'https://www.guojiz.com/';
//获取远程网页的编码
function _charset($url){
$text = file_get_contents($url);
$mode = '/charset=(.*)\"/iU';
preg_match($mode,$text,$result);
return $result[1];
}
$charset = _charset($url);//获取远程网页的编码
//以上为公用
//1:获取远程网页的标题,$url地址,$charset用于判断编码
function _title($url,$charset){
$text = file_get_contents($url);
//如果是gb2312编码处理乱码
if ($charset == 'gb2312'){
$text = iconv('gb2312','utf-8',$text);
}
$mode = '/<title>(.*)<\/title>/iU';
preg_match($mode,$text,$result);
return $result[1];
}
echo '网站标题:'.$title = _title($url,$charset); //获取远程网页的标题
//2:获取远程网页的简介
function _description($url,$charset){
$text = file_get_contents($url);
//gb2312编码乱码处理
if ($charset == 'gb2312'){
$text = iconv('gb2312','utf-8',$text);
}
$mode = '/<meta\s+name=\"description\"\s+content=\"(.*)\"\s?\/?>/iU';
preg_match($mode,$text,$result);
return $result[1];
}
echo '网站简介:'.$description = _description($url,$charset); //获取远程网页的简介