基于Snoopy的PHP近似完美获取网站编码的代码


Posted in PHP onOctober 23, 2011

先要到网上下载Snoopy.class.php
调用方法:

<?php 
require 'lib/Snoopy.class.php'; 
require 'lib/WebCrawl.class.php';//包含下面代码 
$go=new WebCrawl('http://www.baidu.com'); 
echo $go->getCharset(); 
?>

<?php 
class WebCrawl 
{ 
private $url; 
private $request; 
public $charset_arr=array( 
'gb2312', 
'utf-8', 
'big5', 
'gbk', 
'ascii', 
'cp936', 
'ibm037', 
'ibm437', 
'ibm500', 
'asmo-708', 
'dos-720', 
'ibm737', 
'ibm775', 
'ibm850', 
'ibm852', 
'ibm855', 
'ibm857', 
'ibm00858', 
'ibm861', 
'ibm860', 
'dos-862', 
'ibm863', 
'ibm864', 
'ibm865', 
'cp866', 
'ibm869', 
'ibm870', 
'windows-874', 
'cp875', 
'shift_jis', 
'ks_c_5601-1987', 
'ibm1026', 
'ibm01047', 
'ibm01047', 
'ibm01040', 
'ibm01041', 
'ibm01042', 
'ibm01043', 
'ibm01044', 
'ibm01045', 
'ibm01046', 
'ibm01047', 
'ibm01048', 
'ibm01049', 
'utf-16', 
'unicodefffe', 
'windows-1250', 
'windows-1251', 
'windows-1252', 
'windows-1253', 
'windows-1254', 
'windows-1255', 
'windows-1256', 
'windows-1257', 
'windows-1258', 
'johab', 
'macintosh', 
'x-mac-japanese', 
'x-mac-chinesetrad', 
'x-mac-korean', 
'x-mac-arabic', 
'x-mac-hebrew', 
'x-mac-greek', 
'x-mac-cyrillic', 
'x-mac-chinesesimp', 
'x-mac-romanian', 
'x-mac-ukrainian', 
'x-mac-thai', 
'x-mac-ce', 
'x-mac-icelandic', 
'x-mac-turkish', 
'x-mac-croatian', 
'x-chinese-cns', 
'x-cp20001', 
'x-chinese-eten', 
'x-cp20003', 
'x-cp20004', 
'x-cp20005', 
'x-ia5', 
'x-ia5-german', 
'x-ia5-swedish', 
'x-ia5-norwegian', 
'us-ascii', 
'x-cp20261', 
'x-cp20269', 
'ibm273', 
'ibm277', 
'ibm278', 
'ibm280', 
'ibm284', 
'ibm285', 
'ibm290', 
'ibm420', 
'ibm423', 
'ibm424', 
'x-ebcdic-koreanextended', 
'ibm-thai', 
'koi8-r', 
'ibm871', 
'ibm880', 
'ibm905', 
'ibm00924', 
'x-cp20936', 
'x-cp20949', 
'cp1025', 
'koi8-u', 
'iso-8859-1', 
'iso-8859-2', 
'iso-8859-3', 
'iso-8859-4', 
'iso-8859-5', 
'iso-8859-6', 
'iso-8859-7', 
'iso-8859-8', 
'iso-8859-9', 
'iso-8859-13', 
'iso-8859-15', 
'x-europa', 
'iso-8859-8-i', 
'iso-2022-jp', 
'csiso2022jp', 
'iso-2022-jp', 
'iso-2022-kr', 
'x-cp50227', 
'euc-jp', 
'euc-cn', 
'euc-kr', 
'hz-gb-2312', 
'gb18030', 
'x-iscii-de', 
'x-iscii-be', 
'x-iscii-ta', 
'x-iscii-te', 
'x-iscii-as', 
'x-iscii-or', 
'x-iscii-ka', 
'x-iscii-ma', 
'x-iscii-gu', 
'x-iscii-pa', 
'utf-7', 
'utf-32', 
'utf-32be' 
); 
public function __construct($url) 
{ 
$this->url=$url; 
} 
//打开网站 
private function open($url) 
{ 
if($this->request!==null) 
{ 
if($this->request->status==200) 
{ 
return true; 
} 
else 
{ 
return false; 
} 
} 
else 
{ 
$this->request=new Snoopy(); 
$this->request->fetch($url); 
if($this->request->status==200) 
{ 
$this->request->results=strtolower($this->request->results); 
$charset=$this->getCharset(); 
if($charset!="utf-8") 
{ 
if($charset=="windows-1252") 
{ 
$this->request->results=$this->uni_decode($this->request->results); 
} 
else 
{ 
$this->request->results=mb_convert_encoding($this->request->results,"UTF-8",$charset); 
} 
} 
return true; 
} 
else 
{ 
return false; 
} 
} 
} 
//获取网站title,keywords,description 
public function getWebinfo() 
{ 
$info=array( 
'title'=>'', 
'keywords'=>'', 
'desc'=>'', 
'ip'=>'' 
); 
if(!$this->open($this->url)){return $info;exit;} 
// print_r($this->request->results);exit; 
preg_match('/<title>([^>]*)<\/title>/si', $this->request->results, $titlematch ); 
if (isset($titlematch) && is_array($titlematch) && count($titlematch) > 0) 
{ 
$info['title'] = strip_tags($titlematch[1]); 
} 
preg_match_all('/<[\s]*meta[\s]*name="?' . '([^>"]*)"?[\s]*' . 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $this->request->results, $match); 
$ft=0; 
foreach($match[1] as $mt) 
{ 
if($mt=="keywords" || $mt=="description") 
{ 
$ft=1; 
} 
} 
if($ft==0) 
{ 
preg_match_all('/<[\s]*meta[\s]*content="?([^>"]*)"?[\s]*name="?' . '([^>"]*)"?[\s]*[\/]?[\s]*>/si', $this->request->results, $match); 
if (isset($match) && is_array($match) && count($match) == 3) 
{ 
$originals = $match[0]; 
$names = $match[2]; 
$values = $match[1]; 
if (count($originals) == count($names) && count($names) == count($values)) 
{ 
$metaTags = array(); 
for ($i=0, $limiti=count($names); $i < $limiti; $i++) 
{ 
$metaTags[$names[$i]] = array ( 
'html' => htmlentities($originals[$i]), 
'value' => $values[$i] 
); 
} 
} 
} 
} 
else 
{ 
if (isset($match) && is_array($match) && count($match) == 3) 
{ 
$originals = $match[0]; 
$names = $match[1]; 
$values = $match[2]; 
if (count($originals) == count($names) && count($names) == count($values)) 
{ 
$metaTags = array(); 
for ($i=0, $limiti=count($names); $i < $limiti; $i++) 
{ 
$metaTags[$names[$i]] = array ( 
'html' => htmlentities($originals[$i]), 
'value' => $values[$i] 
); 
} 
} 
} 
} 
$result = array ( 
'metaTags' => $metaTags 
); 
if(isset($result['metaTags']['keywords']['value'])) 
{ 
$info['keywords']=$result['metaTags']['keywords']['value']; 
} 
else 
{ 
$info['keywords']=""; 
} 
if(isset($result['metaTags']['description']['value'])) 
{ 
$info['desc']=$result['metaTags']['description']['value']; 
} 
else 
{ 
$info['desc']=""; 
} 
$domain=preg_replace('/http\:\/\//si', '', $this->url); 
$ip=@gethostbyname($domain); 
$ip_arr=explode(".", $ip); 
if(count($ip_arr)==4) 
{ 
$info['ip']=$ip; 
} 
return $info; 
} 
public function t($string,$o) 
{ 
for($i=0;$i<strlen($string);$i++) 
{ 
if(ord($string{$i})<128) 
continue; 
if((ord($string{$i})&224)==224) 
{ 
//第一个字节判断通过 
$char = $string{++$i}; 
if((ord($char)&128)==128) 
{ 
//第二个字节判断通过 
$char = $string{++$i}; 
if((ord($char)&128)==128) 
{ 
$encoding = "UTF-8"; 
break; 
} 
} 
} 
if((ord($string{$i})&192)==192) 
{ 
//第一个字节判断通过 
$char = $string{++$i}; 
if((ord($char)&128)==128) 
{ 
//第二个字节判断通过 
$encoding = "GB2312"; 
break; 
} 
} 
} 
return strtolower($encoding); 
} 
function uni_decode ($str, $code = 'utf-8'){ 
$str = json_decode(preg_replace_callback('/&#(\d{5});/', create_function('$dec', 'return \'\\u\'.dechex($dec[1]);'), '"'.$str.'"')); 
if($code != 'utf-8'){ $str = iconv('utf-8', $code, $str); } 
return $str; 
} 
//获取网站编码 
public function getCharset() 
{ 
if(!$this->open($this->url)){return false;exit;} 
//首先从html获取编码 
preg_match("/<meta.+?charset=[^\w]?([-\w]+)/i",$this->request->results,$temp) ? strtolower($temp[1]):""; 
if($temp[1]!="") 
{ 
if(in_array($temp[1], $this->charset_arr)) 
{ 
if($temp[1]=="gb2312") 
{ 
$tmp_charset=$this->t($this->request->results,$temp[1]); 
if($tmp_charset==$temp[1]) 
{ 
return $temp[1]; 
} 
} 
else 
{ 
return $temp[1]; 
} 
} 
} 
if(!empty($this->request->headers)) 
{ 
//从header中获取编码 
$hstr=strtolower(implode("|||",$this->request->headers)); 
preg_match("/charset=[^\w]?([-\w]+)/is",$hstr,$lang) ? strtolower($lang[1]):""; 
if($lang[1]!="") 
{ 
return $lang[1]; 
} 
} 
$encode_arr=array("UTF-8","GB2312","GBK","BIG5","ASCII","EUC-JP","Shift_JIS","CP936","ISO-8859-1","JIS","eucjp-win","sjis-win"); 
$encoded=mb_detect_encoding($this->request->results,$encode_arr); 
if($encoded) 
{ 
return strtolower($encoded); 
} 
else 
{ 
return false; 
} 
} 
} 
?>
PHP 相关文章推荐
PHP文本数据库的搜索方法
Oct 09 PHP
mysql建立外键
Nov 25 PHP
PHP URL地址获取函数代码(端口等) 推荐
May 15 PHP
PHP session会话的安全性分析
Sep 08 PHP
如何用PHP实现插入排序?
Apr 10 PHP
php保存二进制原始数据为图片的程序代码
Oct 14 PHP
PHP获取服务器端信息的方法
Nov 28 PHP
PHP中使用xmlreader读取xml数据示例
Dec 29 PHP
Windows2003下php5.4安装配置教程(Apache2.4)
Jun 30 PHP
php使用 readfile() 函数设置文件大小大小的方法
Aug 11 PHP
PHP实现简单用户登录界面
Oct 23 PHP
laravel 框架结合关联查询 when()用法分析
Nov 22 PHP
php中经典方法实现判断多维数组是否为空
Oct 23 #PHP
PHP禁止页面缓存的代码
Oct 23 #PHP
Pain 全世界最小最简单的PHP模板引擎 (普通版)
Oct 23 #PHP
供参考的 php 学习提高路线分享
Oct 23 #PHP
PHP中的strtr函数使用介绍(str_replace)
Oct 20 #PHP
PHP中读写文件实现代码
Oct 20 #PHP
Array of country list in PHP with Zend Framework
Oct 17 #PHP
You might like
模拟OICQ的实现思路和核心程序(三)
2006/10/09 PHP
PHP 出现乱码和Sessions验证问题的解决方法!
2008/12/06 PHP
Linux Apache PHP Oracle 安装配置(具体操作步骤)
2013/06/17 PHP
Java和PHP在Web开发方面对比分析
2015/03/01 PHP
使用PHP生成二维码的方法汇总
2015/07/22 PHP
PHP+HTML+JavaScript+Css实现简单爬虫开发
2016/03/28 PHP
php设计模式之迭代器模式实例分析【星际争霸游戏案例】
2020/04/07 PHP
PHP程序员简单的开展服务治理架构操作详解(三)
2020/05/14 PHP
Prototype Array对象 学习
2009/07/19 Javascript
JavaScript 序列化对象实现代码
2009/12/18 Javascript
使用JavaScript 实现的人脸检测
2015/03/24 Javascript
基于jquery实现一个滚动的分步注册向导-附源码
2015/08/26 Javascript
JavaScript  cookie 跨域访问之广告推广
2016/04/20 Javascript
浅谈JavaScript的push(),pop(),concat()方法
2016/06/03 Javascript
javascript 将共享属性迁移到原型中去的实现方法
2016/08/31 Javascript
Javascript ES6中数据类型Symbol的使用详解
2017/05/02 Javascript
Vue 解决路由过渡动画抖动问题(实例详解)
2020/01/05 Javascript
json.stringify()与json.parse()的区别以及用处
2021/01/25 Javascript
[06:35]2014DOTA2国际邀请赛 老男孩梦圆西雅图中国军团世界最强
2014/07/22 DOTA
python私有属性和方法实例分析
2015/01/15 Python
在Django的URLconf中使用命名组的方法
2015/07/18 Python
python pandas dataframe 行列选择,切片操作方法
2018/04/10 Python
Python装饰器用法实例总结
2018/05/26 Python
Python爬虫文件下载图文教程
2018/12/23 Python
对Python Class之间函数的调用关系详解
2019/01/23 Python
Django框架用户注销功能实现方法分析
2019/05/28 Python
教育学专业毕业生的自我评价
2013/11/21 职场文书
四年大学生活的个人自我评价
2013/12/11 职场文书
团日活动总结书格式
2014/05/08 职场文书
乡镇四风对照检查材料
2014/08/31 职场文书
2014年职称评定工作总结
2014/11/26 职场文书
2015年大学教师工作总结
2015/05/20 职场文书
政审证明范文
2015/06/19 职场文书
公司庆典主持词
2015/07/04 职场文书
篮球比赛通讯稿
2015/07/18 职场文书
2015年小学师德师风建设工作总结
2015/10/23 职场文书