php字符串截长(支持中文)
<?php /** * 字符串截长(支持中文) * @author:zms * @version:2011-7-27 */ /** * 字符串截取 * @param object $str : 字符串(支持中文) * @param object $start :截取开始位置 * @param object $length:截取结束位置 * @param object $encode [optional]:字符串编码 * @param object $input_encode [optional]:输入的文字的编码 * @return */ function substring($str, $start, $length, $encode = 'utf-8', $input_encode = 'utf-8') { //编码转换 $str = iconv($input_encode, $encode, $str); //正则匹配 preg_match_all(getRege($encode), $str, $match); //从数组取得数据,组成字符串 $slice = join("", array_slice($match[0], $start, $length)); return $slice; } /** * 中文编码 * @param object $type * @return */ function getRege($type) { $rege = ""; switch (strtolower($type)) { case 'utf-8': $rege = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; break; case 'gb2312': $rege = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; break; case 'gbk': $rege = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; break; case "big5": $rege = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; break; default: echo "charset error"; exit; } return $rege; } ?>
相关推荐
amberom 2020-08-03
有梦就能实现 2020-06-10
缘起宇轩阁 2019-12-21
xcguoyu 2019-10-29
缘起宇轩阁 2019-10-28
amberom 2019-10-25
yangguangdblu 2019-06-18
drise 2013-11-25
xianzhe 2019-06-30
zhangkala 2013-06-08
技术渣 2016-10-07
Phplayers 2019-06-28
wangdoudou0 2015-07-07
spinachcqb 2019-06-27
会写code的凳子哥 2013-11-21
静心斋 2013-08-23
技术渣 2012-08-07
会写code的凳子哥 2012-07-06
会写code的凳子哥 2011-07-18