8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png
教程:获取网站标题、关键词、简介 PHP 只有教程
admin 2020-8-2

用PHP的方法简单获取的几行代码,获取网站标题、关键词、简介。

<?php
  if ($_GET['url']) {
  $site='http://';
  $url=trim($site.$_GET['url']);
  $info=file_get_contents($url);
  header('Content-type:text/json');function _charset($url){
  $text = file_get_contents($url);
  $mode = '/charset=(.*)\"/iU';
  preg_match($mode,$text,$result);
  return $result[1];}$charset = _charset($url);function _title($url,$charset){
  $text = file_get_contents($url);
  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);echo "\n";function _keywords($url,$charset){
  $text = file_get_contents($url);
  if ($charset == 'gb2312'){
   $text = iconv('gb2312','utf-8',$text);
  }
  $mode = '/<meta\s+name=\"keywords\"\s+content=\"(.*)\"\s?\/?>/iU';
  preg_match($mode,$text,$result);
  return $result[1];}echo '网站关键词:'.$keywords = _keywords($url,$charset);echo "\n";function _description($url,$charset){
  $text = file_get_contents($url);
  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);}
  else
	  echo "URL参数为空,请传入URL参数。";

示例xxx.cn/xx.php?url=xxx.cn

最新回复 (1)
    • 朕弟分享 | 专注小众,乐于分享!
      3
          
返回
发新帖 搜索 反馈 回顶部