<?php
/**
* 分割字符串
* @param String $str 要分割的字符串
* @param int $length 指定的长度
* @param String $end 在分割后的字符串块追加的内容
*/
function mb_chunk_split($string, $length, $end, $once = false){
$string =
iconv('gb2312', 'utf-8//ignore', $string);
$array = array();
$strlen = mb_strlen($string);
while($strlen){
$array[] = mb_substr($string, 0, $length, "utf-8");
if($once)
return $array[0] . $end;
$string = mb_substr($string, $length, $strlen, "utf-8");
$strlen = mb_strlen($string);
}
return implode($end, $array);
}
$str = 's六一马上$就dfs要到$@#了';
$str1 = 'aabbccddeefff';
echo mb_chunk_split($str, 3, '...', true); //s六一...马上$...就df...s要到...$@#...了
echo "<br>";
echo mb_chunk_split($str1, 2, '...'); //aa...bb...cc...dd...ee...ff...f