基于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 相关文章推荐
用文本文件实现的动态实时发布新闻的程序
Oct 09 PHP
PHP获取网站域名和地址的代码
Aug 17 PHP
如何利用PHP执行.SQL文件
Jul 05 PHP
php实现的一个简单json rpc框架实例
Mar 30 PHP
PHP多维数组遍历方法(2种实现方法)
Dec 10 PHP
Smarty保留变量用法分析
May 23 PHP
php实现URL加密解密的方法
Nov 17 PHP
PHP简单实现循环链表功能示例
Nov 10 PHP
PHP中的empty、isset、isnull的区别与使用实例
Mar 22 PHP
PHP使用Session实现上传进度功能详解
Aug 06 PHP
php桥接模式应用案例分析
Oct 23 PHP
laravel 框架执行流程与原理简单分析
Feb 01 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
使用php实现快钱支付功能(涉及到接口)
2013/07/01 PHP
PHP实现数组递归转义的方法
2014/08/28 PHP
PHP类的声明与实例化及构造方法与析构方法详解
2016/01/26 PHP
PHP框架Laravel插件Pagination实现自定义分页
2020/04/22 PHP
Laravel 集成微信用户登录和绑定的实现
2019/12/27 PHP
jquery构造器的实现代码小结
2011/05/16 Javascript
JavaScript ECMA-262-3 深入解析.第三章.this
2011/09/28 Javascript
找出字符串中出现次数最多的字母和出现次数精简版
2012/11/07 Javascript
使用原生js实现页面蒙灰(mask)效果示例代码
2014/06/20 Javascript
EasyUI,点击开启编辑框,并且编辑框获得焦点的方法
2015/03/01 Javascript
js实现类似于add(1)(2)(3)调用方式的方法
2015/03/04 Javascript
JavaScript String 对象常用方法详解
2016/05/13 Javascript
JavaScript运动框架 多值运动(四)
2017/05/18 Javascript
微信小程序基于slider组件动态修改标签透明度的方法示例
2017/12/04 Javascript
vue项目中公用footer组件底部位置的适配问题
2018/05/10 Javascript
微信小程序背景音乐开发详解
2019/12/12 Javascript
微信小程序实现点击导航条切换页面
2020/11/19 Javascript
[02:51]DOTA2战队出征照拍摄花絮 TI3明星化身时尚男模
2013/07/22 DOTA
python使用Flask框架获取用户IP地址的方法
2015/03/21 Python
浅谈Python peewee 使用经验
2017/10/20 Python
python tkinter图形界面代码统计工具(更新)
2019/09/18 Python
python检查目录文件权限并修改目录文件权限的操作
2020/03/11 Python
分享一枚pycharm激活码适用所有pycharm版本我的pycharm2020.2.3激活成功
2020/11/20 Python
Java的五个基础面试题
2016/02/26 面试题
开办化妆品公司创业计划书
2013/12/26 职场文书
项目副经理岗位职责
2013/12/30 职场文书
2014年庆元旦活动方案
2014/02/15 职场文书
《新型玻璃》教学反思
2014/04/13 职场文书
员工激励培训演讲稿
2014/09/16 职场文书
2014年银行年终工作总结
2014/12/19 职场文书
2015年学生会工作总结范文
2015/03/31 职场文书
护士长2015年终工作总结
2015/04/24 职场文书
Python排序算法之插入排序及其优化方案详解
2021/06/11 Python
攻略丨滑雪究竟该选哪款对讲机?
2022/02/18 无线电
详解python的异常捕获
2022/03/03 Python
Python内置类型集合set和frozenset的使用详解
2022/04/26 Python