Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15176
  • 博文数量: 11
  • 博客积分: 130
  • 博客等级: 入伍新兵
  • 技术积分: 130
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-07 13:21
文章分类

全部博文(11)

文章存档

2015年(1)

2014年(5)

2011年(5)

我的朋友
最近访客

分类: 系统运维

2011-04-09 14:49:20

  1. /**
  2.  * 处理字符串类,旨在练习字符串等
  3.  */
  4. class ProcessString {
  5.     
  6.     var $string;
  7.     var $slice_array = array();
  8.     var $charset = array();
  9.     var $CHARSET=array(
  10.         "UTF-8",
  11.         "GBK",
  12.         "ACSII"
  13.     );
  14.     var $length;
  15.     
  16.     function __construct($string) {
  17.         $this->string = $string;
  18.         $this->length = array(
  19.             strlen($this->string),
  20.             strlen($this->string)
  21.         );
  22.         $this->slice();
  23.     }
  24.     
  25.     /*通过魔术方法调用系统函数,省略系统函数指定字符串的参数
  26.      * strstr('hello','h');
  27.      * ----->
  28.      * $ps=new ProcessString('hello');
  29.      * echo $ps->strstr('h');
  30.      */
  31.     public function __call($function, $args) {
  32.         if (function_exists($function)) {
  33.             array_unshift($args, $this->string);
  34.             return call_user_func_array($function, $args);
  35.         } else {
  36.             echo "Called method/function :$function not exists! Params:";
  37.             print_r($args);
  38.         }
  39.     }
  40.     
  41.     public function slice() {
  42.         $len=$I = 0;
  43.         $String=$this->string;
  44.         $Length = $this->length[1];
  45.         while ($len++ < $Length) {
  46.             $StringTMP = substr($String, $I, 1);
  47.             if (ord($StringTMP) >= 224) {
  48.                 $StringTMP = substr($String, $I, 3);
  49.                 $I = $I + 3;
  50.                 $this->length[1]-=2;
  51.                 $this->hasCharset($this->CHARSET[0]);
  52.             } elseif (ord($StringTMP) >= 192) {
  53.                 $StringTMP = substr($String, $I, 2);
  54.                 $I = $I + 2;
  55.                 $this->length[1]-=1;
  56.                 $this->hasCharset($this->CHARSET[1]);
  57.             } else {
  58.                 $I = $I + 1;
  59.                 $this->hasCharset($this->CHARSET[2]);
  60.             }
  61.             $Length=$this->length[1];
  62.             $this->slice_array[] = $StringTMP;
  63.         }
  64.     }
  65.     
  66.     public function hasCharset($charset){
  67.         if(!in_array($charset, $this->charset)){
  68.             array_push($this->charset, $charset);
  69.         }
  70.     }
  71.     
  72.     public function get_str($length,$omit=true,$tipstring='...'){
  73.         $res_arr = $this->slice_array;
  74.         if($length<$this->length[1]){
  75.             $res_arr=array_slice($res_arr, 0,$length);
  76.         }else{
  77.             $omit=true;
  78.         }
  79.         return implode('', $res_arr).($omit?null:$tipstring);
  80.     }
  81.     
  82.     /*根据运行环境CGI/CLI,判断换行符*/
  83.     public static function newline(){
  84.         $newline = isset($_SERVER['GATEWAY_INTERFACE'])?'
    '
    :"\n";
  85.         return $newline;
  86.     }
  87. }
    1. $teststring = "a通过。f魔术df ,。方法调用系统函数";
    2. $ps = new ProcessString($teststring);
    3. echo "字符串[{$ps->string}]长度:{$ps->length[0]} {$ps->length[1]}";
    4. echo $ps->newline();
    5. echo '取出前五个为:'.$ps->get_str(5,false);
    6. echo $ps->newline();
result:
字符串[a通过。f魔术df ,。方法调用系统函数]长度:48 20
取出前五个为:a通过。f...





阅读(396) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:ubuntu ibus-setup software-center 段错误

给主人留下些什么吧!~~