基于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数据库的PHP程序
Oct 09 PHP
PHP 出现乱码和Sessions验证问题的解决方法!
Dec 06 PHP
php连接mysql数据库代码
Mar 10 PHP
PHP原生模板引擎 最简单的模板引擎
Apr 25 PHP
朋友网关于QQ相关的PHP代码(研究QQ的绝佳资料)
Jan 26 PHP
php单文件版在线代码编辑器
Mar 12 PHP
php获取网站百度快照日期的方法
Jul 29 PHP
PHP的Yii框架的基本使用示例
Aug 21 PHP
微信支付开发交易通知实例
Jul 12 PHP
php获取用户真实IP和防刷机制的实例代码
Nov 28 PHP
PHP 对接美团大众点评团购券(门票)的开发步骤
Apr 03 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作的文本留言本的例子(三)
2006/10/09 PHP
PHP5+UTF8多文件上传类
2008/10/17 PHP
PHP面向对象分析设计的61条军规小结
2010/07/17 PHP
php number_format() 函数通过千位分组来格式化数字的实现代码
2013/08/06 PHP
wordpress自定义标签云与随机获取标签的方法详解
2019/03/22 PHP
PHP pthreads v3在centos7平台下的安装与配置操作方法
2020/02/21 PHP
刷新页面实现方式总结(HTML,ASP,JS)
2008/11/13 Javascript
js+xml生成级联下拉框代码
2012/07/24 Javascript
使用jQuery将多条数据插入模态框的实现代码
2014/10/08 Javascript
jQuery提示效果代码分享
2014/11/20 Javascript
node.js回调函数之阻塞调用与非阻塞调用
2015/11/13 Javascript
JS快速实现移动端拼图游戏
2016/09/05 Javascript
详谈angularjs中路由页面强制更新的问题
2017/04/24 Javascript
浅谈Node.js ORM框架Sequlize之表间关系
2017/07/24 Javascript
vue.js组件vue-waterfall-easy实现瀑布流效果
2017/08/22 Javascript
详解基于iview-ui的导航栏路径(面包屑)配置
2019/02/22 Javascript
vue项目添加多页面配置的步骤详解
2019/05/22 Javascript
Python实现遍历windows所有窗口并输出窗口标题的方法
2015/03/13 Python
Python中的类与对象之描述符详解
2015/03/27 Python
对Xpath 获取子标签下所有文本的方法详解
2019/01/02 Python
python redis 删除key脚本的实例
2019/02/19 Python
python tkinter组件使用详解
2019/09/16 Python
Python中os模块功能与用法详解
2020/02/26 Python
python基于socket函数实现端口扫描
2020/05/28 Python
Ubuntu 20.04安装Pycharm2020.2及锁定到任务栏的问题(小白级操作)
2020/10/29 Python
在Ubuntu中安装并配置Pycharm教程的实现方法
2021/01/06 Python
Python实现邮件发送的详细设置方法(遇到问题)
2021/01/18 Python
python程序实现BTC(比特币)挖矿的完整代码
2021/01/20 Python
韩国演唱会订票网站:StubHub韩国
2019/01/17 全球购物
Timberland澳大利亚官网:全球领先的户外品牌
2019/12/10 全球购物
同学聚会策划方案
2014/06/06 职场文书
工程学毕业生自荐信
2014/06/14 职场文书
法院反腐倡廉心得体会
2014/09/09 职场文书
齐云山导游词
2015/02/06 职场文书
2016优秀大学生个人事迹材料范文
2016/03/01 职场文书
python套接字socket通信
2022/04/01 Python