utf8终极截字类---来自phpchina的geniuscynic

本文介绍了一个用于处理UTF-8编码字符串的PHP类,该类提供了计算字符串长度及截取字符串等功能,解决了之前版本存在的乱码问题,并且功能更加完善。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上三个版本的都或解决了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 );
          }
          elseif (
$aciss  >=  224  )
          {
               
$out_str .= substr  $str 0 );
          }
          elseif (
$aciss  >=  192  )
          {
               
$out_str .= substr  $str 0 );
          }
          else
          {
               
$out_str .= substr  ( $str 0 );
          }
          return 
$out_str ;
     }

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

          if (
$aciss  >=  240  )
          {
               
$out_str .= substr  $str );
          }
          elseif (
$aciss  >=  224  )
          {
               
$out_str .=  substr  $str );
          }
          elseif (
$aciss  >=  192  )
          {
               
$out_str .=  substr  $str );
          }
          else
          {
               
$out_str .=  substr  ( $str );
          }

          return 
$out_str ;
     }

     public function 
count_word ( $str $length = )
     {
          
$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  = - )
     {
          
$temp  '' ;
          
          if(
$start  )
          {
               
$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  == - )
          {
               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) "^&"
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值