<?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; } ?>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号