多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
一、字符串截取 /** * 字符串截取 * @param unknown $str * @param unknown $length * @param string $end_with * @return unknown|string */ function utf8_substr_ifneed($str, $length, $end_with='…') { if( strlen($str) <= $length ) return $str; //正则 $re_utf8 = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; preg_match_all($re_utf8, $str, $match); $new_str = ""; $now_length = 0; $max_length = $length - (strlen($end_with)-1); foreach($match[0] as $char) { $now_length += (strlen($char)>1) ? 2 :1 ;//英文字符长度,汉字算两个 if( $now_length>$max_length ) break; $new_str .= $char; } return $new_str.$end_with; }