发新话题
打印

utf8终极截字类

utf8终极截字类

复制内容到剪贴板
代码:
上三个版本的都或解决了utf8 下截字的乱码问题,但功能不是很全面,这次贴出的在功能上和substr已经不相上下了,代码如下:
<?php
class splite_utf8
{
     private  function splite_single_utf8_left_word ($str )
     {
          $aciss = ord( $str);
          $out_str = '';

          if ($aciss >= 240 )
          {
               $out_str.=substr ( $str, 0, 4 );
          }
          elseif ($aciss >= 224 )
          {
               $out_str.=substr ( $str, 0, 3 );
          }
          elseif ($aciss >= 192 )
          {
               $out_str.=substr ( $str, 0, 2 );
          }
          else
          {
               $out_str.=substr ($str, 0, 1 );
          }
          return $out_str;
     }

     private  function splite_single_utf8_right_word ($str )
     {
          $aciss = ord( $str);
          $out_str = '';

          if ($aciss >= 240 )
          {
               $out_str.=substr ( $str, 4 );
          }
          elseif ($aciss >= 224 )
          {
               $out_str.= substr ( $str, 3 );
          }
          elseif ($aciss >= 192 )
          {
               $out_str.= substr ( $str, 2 );
          }
          else
          {
               $out_str.= substr ($str, 1 );
          }

          return $out_str;
     }

     public function count_word($str, $length=0 )
     {
          $aciss = ord( $str);

          if ($aciss >= 240 )
          {
               $length+= 1;
               $str=substr($str,4);
          }
          elseif ($aciss >= 224 )
          {
               $length+= 1;
               $str=substr($str,3);
          }
          elseif ($aciss >= 192 )
          {
               $length+= 1;
               $str=substr($str,2);
          }
          else
          {
               $length+= 1;
               $str=substr($str,1);
          }

          if($str=='')
          {
               return $length;
          }
          else
          {
               return $this->count_word($str,$length);
          }
     }
     
     public function splite_mulit_utf8_word ($str, $start = 0, $length = -1 )
     {
          $temp = '';
         
          if($start < 0 )
          {
               $start = $this->count_word($str) + $start;     
          }
         
          for ($i = 0; $i < $start; $i++ )
          {
               $str=$this->splite_single_utf8_right_word ($str );
          }

          for ($i = 0; $i < $length; $i++ )
          {
               $temp.= $this->splite_single_utf8_left_word ($str );
               $str = $this->splite_single_utf8_right_word ($str );
          }

          if( $length == -1 )
          {
               return $str;
          }
          else
          {
               return $temp;
          }
     }
}

$utf=new splite_utf8();
$text='的萨芬dfdf!@#$%^&*I()';
$length=$utf->count_word($text);
echo $length."\n";
$word=$utf->splite_mulit_utf8_word ($text, -6, 2);
var_dump($word);
?>

屏幕输出的就是
18
string(2) "^&"
交流QQ群2:16142493
智能手机软件下载
PHP新手不可错过一帖
PHP新手如何获得积分
论坛需要你,我们大家需要你!

TOP

up!~~~藏了,哈哈
我不是天使但我拥有天堂,我不是海豚但我翱翔海洋,我没有翅膀但我俯视阳光,我没有三叶草但我手捧希望...

TOP

发新话题