中文汉字的正则字符编码范围

Filed under: Spider | No Comments »
Posted on

中文编码范围,中文汉字的正则也许用的着。

双字节字符编码范围:

1. GBK (GB2312/GB18030)

\x00-\xff GBK双字节编码范围
\x20-\x7f ASCII
\xa1-\xff 中文gb2312
\x80-\xff 中文 gbk

2. UTF-8 (Unicode)

\u4e00-\u9fa5 (中文)
\x3130-\x318F (韩文)
\xAC00-\xD7A3 (韩文)
\u0800-\u4e00 (日文)

PHP完整删除文件夹函数

Filed under: PHP&MySQL | No Comments »
Posted on
  1. /**
  2. * 删除指定目录
  3. *
  4. * @param string $dirname 路径
  5. * @return void
  6. */
  7. function rmdir_rf($dirname) {
  8.     if ($dirHandle = opendir($dirname)) {
  9.         chdir($dirname);
  10.         //该处很多地方都写$file = readdir($dirHandle)),当$file = 0时,函数失效
  11.         while (false !== ($file = readdir($dirHandle))) {
  12.             if ($file == '.' || $file == '..') continue;
  13.             if (is_dir($file))rmdir_rf($file);
  14.             else unlink($file);
  15.         }
  16.         chdir('..');
  17.         rmdir($dirname);
  18.         closedir($dirHandle);
  19.     }
  20. }

该函数在PHP手册中有人写过,但当while语句中的遍历写的有点小问题,在使用中发现函数失效,就随后记下来吧,好像一年多没更新过博客了,惭愧……

Snoopy中文手册(毛毛虫翻译)

Filed under: Spider | 9 Comments »
Posted on
  1. /**
  2. * @name Snoopy手册中文版
  3. * @author 毛毛虫 wangchong1985@gmail.com
  4. * @version Snoopy - the PHP net client v1.2.2
  5. * @link http://www.wangchong.org
  6. * @since 2008-04-27
  7. */

名称:

Snoopy - the PHP net client v1.2.2

概要:

  1. include "Snoopy.class.php";
  2.     $snoopy = new Snoopy;
  3.    
  4.     $snoopy->fetchtext("http://www.php.net/");
  5.     print $snoopy->results;
  6.    
  7.     $snoopy->fetchlinks("http://www.phpbuilder.com/");
  8.     print $snoopy->results;
  9.    
  10.     $submit_url = "http://lnk.ispi.net/texis/scripts/msearch/netsearch.html";
  11.    
  12.     $submit_vars["q"] = "amiga";
  13.     $submit_vars["submit"] = "Search!";
  14.     $submit_vars["searchhost"] = "Altavista";
  15.        
  16.     $snoopy->submit($submit_url,$submit_vars);
  17.     print $snoopy->results;
  18.    
  19.     $snoopy->maxframes=5;
  20.     $snoopy->fetch("http://www.ispi.net/");
  21.     echo "<PRE>\n";
  22.     echo htmlentities($snoopy->results[0]);
  23.     echo htmlentities($snoopy->results[1]);
  24.     echo htmlentities($snoopy->results[2]);
  25.     echo "</PRE>\n";
  26.  
  27.     $snoopy->fetchform("http://www.altavista.com");
  28.     print $snoopy->results;

描述:

Snoopy是什么?

Snoopy是一个php类,用来模仿web浏览器的功能,它能完成获取网页内容和发送表单的任务。

Snoopy的一些特点:

* 方便抓取网页的内容
* 方便抓取网页的文本内容 (去除HTML标签)
* 方便抓取网页的链接
* 支持代理主机
* 支持基本的用户名/密码验证
* 支持设置 user_agent, referer(来路), cookies 和 header content(头文件)
* 支持浏览器转向,并能控制转向深度
* 能把网页中的链接扩展成高质量的url(默认)
* 方便提交数据并且获取返回值
* 支持跟踪HTML框架(v0.92增加)
* 支持再转向的时候传递cookies (v0.92增加)
Read the rest of this entry »

仿QQ等级显示函数

Filed under: PHP&MySQL | No Comments »
Posted on

开头先引述下QQ等级的算法:
设当前等级为N,达到当前等级最少需要的活跃天数为D,当前活跃天数为Dc,升级剩余天数为Dr,则:

mimetex.gif

从而推出:

mimetex1.gif

好了,引述完成,懒得写字了,贴出代码:

  1. <?php
  2. /**
  3. *
  4. * showRank.php
  5. * QQ等级输出类库
  6. * @author 毛毛虫 <wangchong1985@gmail.com>
  7. * @version 1.0 2008-04-09
  8. *
  9. */
  10. class showRank {
  11.    
  12.     /**
  13.      * 第一级图片显示字段
  14.      */
  15.     public $mImage1;
  16.    
  17.     /**
  18.      * 第二级图片显示字段
  19.      */
  20.     public $mImage2;
  21.    
  22.     /**
  23.      * 第三级图片显示字段
  24.      */
  25.     public $mImage3;
  26.  
  27.     /**
  28.      * 构造函数:传入图片值
  29.      * @return
  30.      */
  31.     function __construct($pImage1 = '',$pImage2 = '',$pImage3 = '') {
  32.         $this->mImage1 = $pImage1;
  33.         $this->mImage2 = $pImage2;
  34.         $this->mImage3 = $pImage3;
  35.     }
  36.    
  37.     /**
  38.      * 根据活跃天数计算用户等级。(模仿QQ的升级方式)
  39.      * @return int
  40.      * @access public
  41.      */
  42.     function get_rank($pScore) {
  43.         $temp = $pScore+4;
  44.         $tRank = sqrt($temp)-2;
  45.         $tRank = floor($tRank);
  46.         return $tRank;
  47.     }
  48.    
  49.     /**
  50.      * 用户等级标志,根据用户等级显示用户标志
  51.      * 仿照QQ等级的四进制显示
  52.      * @return str
  53.      * @access public
  54.      */
  55.     function get_score($pScore) {
  56.         $str = '';
  57.         $tRank = $this->get_rank($pScore);//根据分数取得等级
  58.         $tPicNum = base_convert($tRank,10,4);//转化为四进制
  59.         $tPicNum = strrev($tPicNum);//翻转字符串
  60.         $tArray = str_split($tPicNum);//转化为数组
  61.         $tNum = count($tArray);
  62.         if($tNum<=3) {
  63.             for($i=$tNum-1;$i>=0;$i--){
  64.                 switch($i){
  65.                     case '0':
  66.                         for($j=0;$j<$tArray[$i];$j++){
  67.                             $str .= $this->mImage1;
  68.                         }
  69.                     break;
  70.                     case '1':
  71.                         for($j=0;$j<$tArray[$i];$j++){
  72.                             $str .= $this->mImage2;
  73.                         }
  74.                     break;
  75.                     case '2':
  76.                         for($j=0;$j<$tArray[$i];$j++){
  77.                             $str .= $this->mImage3;
  78.                         }
  79.                     break;
  80.                     default:
  81.                         //$str = ;
  82.                     break;
  83.                 }
  84.             }
  85.         }else {
  86.             $str =     $this->mImage3.$this->mImage3.$this->mImage3.$this->mImage3;
  87.         }
  88.         return $str;
  89.  
  90.     }
  91.  
  92. }
  93. ?>

Read the rest of this entry »

PHP实现‘服务器推’(flush函数使用)

Filed under: PHP&MySQL | 4 Comments »
Posted on

最近一直在做自己的图书站,分别使用过杰奇和读吧两套不同的系统,对于这两套系统也是又爱又恨,爱的是他们的功能强大,恨他们都同属没有开源精神的产物。呵呵,作为一名穷程序员,俺可以理解作者的苦衷,这里就不批判了。
年假期间,无事可做,翻看自己以前的采集代码,发现很多可以优化和提升的地方,就简单做了下优化,其中也使用了用户体验更加优秀的服务器推技术,这里简单的介绍下核心的函数:flush。
Read the rest of this entry »