PHP 字符截取 解决中文的截取问题,不用mb系列

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

function Cut_string($string, $start ,$sublen, $extstring='...', $code = 'UTF-8') {//Cut_string开始
if($code == 'UTF-8')
{
$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen)).$extstring;
return join('', array_slice($t_string[0], $start, $sublen));
}
else
{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = '';
for($i=0; $i<$strlen; $i++)
{
if($i>=$start && $i<($start+$sublen))
{
if(ord(substr($string, $i, 1))>129)
{
$tmpstr.= substr($string, $i, 2);
}
else
{
$tmpstr.= substr($string, $i, 1);
}
}
if(ord(substr($string, $i, 1))>129) $i++;
}
if(strlen($tmpstr)<$strlen ) $tmpstr.= $extstring;
return $tmpstr;
}
} //Cut_string结束

相关文章

fleaphp crud操作之find函数的使用方法

find函数的原型 复制代码 代码如下: /** * 返回符合条件的第一条记录及所有关联的数据,查询没有结果返回 false * * @param mixed $conditions *...

PHP中对缓冲区的控制实现代码

大家在使用PHP的过程中不免要使用到header和setcookie两个函数,这两个函数会发送一段文件头信息给浏览器,但是如果在使用这两个函数之前已经有了任何输出(包括空输出,比如空格,...

php模拟asp中的XmlHttpRequest实现http请求的代码

类名 :HttpRequest($url="",$method="GET",$useSocket=0) //$url为请求的地址;默认请求方法为GET;$useSocket默认为0,使用...

php图片处理函数获取类型及扩展名实例

本文实例讲述了php图片处理函数获取类型及扩展名的方法。分享给大家供大家参考。 具体实现代码如下: 复制代码 代码如下:image_type=image_type_to_mime_typ...

PHP7新功能总结

以下是小编给大家整理的关于PHP7的相关更新内容和知识点。 新功能 PHP 7增加了许多特性,其中最重要的特性如下所述 • 性能改进——在PHP7中合并了PHPNG代码,速度...