-
-
/** - * Intercept mixed Chinese and English strings
- * 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 round steel, q345e round steel nm360a wear-resistant plate, European standard s355 low alloy plate bs700mc automotive plate, automotive qste460tm structural steel, Ship plate ah36/eh36/dh36, American standard container plate weathering steel spring steel';
- //$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--; //as few as possible
- 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++; //Chinese counts as 2 widths
- /*if($value == ',')
- $j-- ;*/
- $i++;
- }
- //End padding
- 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) ;
- }
- ?>
-
Copy code
|