utf8终极截字类---来自phpchina的geniuscynic
来源:互联网 发布:unity3d 虚拟摇杆图片 编辑:程序博客网 时间:2024/06/15 03:13
上三个版本的都或解决了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) "^&"
<?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) "^&"
- utf8终极截字类---来自phpchina的geniuscynic
- 告那些对学习PHP还不坚定的朋友(转--来自phpchina论坛)
- MySQL终极配置解决UTF8编码出现的乱码问题
- MySQL终极配置解决UTF8编码出现的乱码问题
- MySQL终极配置解决UTF8编码出现的乱码问题
- MySQL终极配置解决UTF8编码出现的乱码问题
- PHP工程师面临的成长瓶颈——来源于PHPchina
- Oracle与MySQL的几点区别 文章出自:PHPChina
- 程序员看法上的几个典型错误(转 作者:phpchina )
- MYSQL使用UTF8中文乱码终极解决办法
- MYSQL使用UTF8中文乱码终极解决办法
- PHPChina上看到的一篇关于网站架构演变的过程,非常不错!!
- 我走了phpchina
- PHPChina留言板实例
- utf8与utf8+bom的区别
- DLL错误之——无法加载DLL"***.dll":找不到指定的模块(异常来自HRESULT:0x8007007E)问题的终极感悟
- utf8网页的问题
- utf8的理解
- [转]Hadoop学习笔记
- 第一次作业
- [转]Getting Started with Hadoop, Part 1
- 巧妙去掉多余的安全删除硬件图标
- mediawiki中,SkinTemplate->template用于设置模板类名称
- utf8终极截字类---来自phpchina的geniuscynic
- 截获的数据
- 如何检测自己是否中招
- 修改Discuz论坛实现对附件下载的限速
- 什么是sid
- 健康指南:人体十大最佳黄金时间
- My New Motion
- SmartNX Mobile(TM) Operating System and NX200 Platform March into the Global Smartphone Arena
- 计算机图书下 tcp/ip协议详解