-
-
/** - * 截取中英文混合的字符串
- * by bbs.it-home.org
- */
- function mb_str_split($string){
- # Split at all position not after the start: ^
- # and not before the end: $
- //$string = iconv('gb2312', 'utf-8//ignore', $string);
- return preg_split('/(?}
$string = 'q345e q345d q345c,q345d圆钢,q345e圆钢nm360a耐磨板,欧标s355低合金板 bs700mc汽车板,汽车qste460tm结构钢,船板ah36/eh36/dh36,美标容器板 耐候钢 弹簧钢';
- //$charlist = mb_str_split($string);
- echo mb_chunk_split($string, 30, '..', true);
- echo '
';
- echo mb_chunk_split($string, 10, '
');
-
- function mb_chunk_split($string, $length, $end = '..', $once = false){
- //$string = iconv('gb2312', 'utf-8//ignore', $string);
- $charlist = mb_str_split($string);
- $i = 0;
- $j = 0;
- $once_array = array();
- foreach($charlist as $value){
- /* if($once){
- if(($i $j) > $length){
- $i--; //尽可能少的
- break;
- }
- }else{
- if(($i $j) >= $length){
- $once_array[] = implode('', array_slice($charlist, $once_leng, $i));
- $once_leng = $i;
- $i = $j = 0;
- }
- } */
-
- if(($i $j) >= $length){
- if($once)
- return implode('', array_slice($charlist, 0, $i-1)) . (count($charlist) <= $i ? '' : $end);
- $once_array[] = $i;
- $i = $j = 0;
- }
-
-
- if(ord($value) > 127)
- $j ; //中文算2个宽度
- /*if($value == ',')
- $j--;*/
- $i ;
- }
- //末尾补齐
- if(array_sum($once_array) < count($charlist))
- $once_array[] = $i;
-
- $str_arr = array();
- $once_leng = 0;
- foreach($once_array as $value){
- $str_arr[] = implode('', array_slice($charlist, $once_leng, $value));
- $once_leng = $value;
- }
-
- return implode($end, $str_arr);
- //return implode('', array_slice($charlist, 0, $i)) . (count($charlist) <= $i ? '' : $end);
- }
- ?>
-
复制代码
|