Home > Backend Development > PHP Tutorial > PHP interception function for mixed Chinese and English strings

PHP interception function for mixed Chinese and English strings

WBOY
Release: 2016-07-25 08:55:04
Original
1203 people have browsed it
  1. /**

  2. * Intercept mixed Chinese and English strings
  3. * by bbs.it-home.org
  4. */
  5. function mb_str_split($string){
  6. # Split at all position not after the start: ^
  7. # and not before the end : $
  8. //$string = iconv('gb2312', 'utf-8//ignore', $string);
  9. return preg_split('/(?}

  10. $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';

  11. //$charlist = mb_str_split($string);
  12. echo mb_chunk_split($string, 30, '..', true);
  13. echo '
    ';
  14. echo mb_chunk_split($string, 10, '
    ');
  15. function mb_chunk_split($string, $length, $end = '..', $once = false){
  16. / /$string = iconv('gb2312', 'utf-8//ignore', $string);
  17. $charlist = mb_str_split($string);
  18. $i = 0;
  19. $j = 0;
  20. $once_array = array ();
  21. foreach($charlist as $value){
  22. /* if($once){
  23. if(($i + $j) > $length){
  24. $i--; //as few as possible
  25. break;
  26. }
  27. }else{
  28. if(($i + $j) >= $length){
  29. $once_array[] = implode('', array_slice($charlist, $once_leng, $i));
  30. $once_leng += $i;
  31. $i = $j = 0;
  32. }
  33. } */
  34. if(($i + $j) >= $length){
  35. if($once)
  36. return implode ('', array_slice($charlist, 0, $i-1)) . (count($charlist) <= $i ? '' : $end);
  37. $once_array[] = $i;
  38. $i = $j = 0;
  39. }
  40. if(ord($value) > 127)
  41. $j++; //Chinese counts as 2 widths
  42. /*if($value == ',')
  43. $j-- ;*/
  44. $i++;
  45. }
  46. //End padding
  47. if(array_sum($once_array) < count($charlist))
  48. $once_array[] = $i;
  49. $str_arr = array();
  50. $once_leng = 0;
  51. foreach($once_array as $value){
  52. $str_arr[] = implode('', array_slice($charlist, $once_leng, $value));
  53. $once_leng += $value;
  54. }
  55. return implode($end, $str_arr);
  56. //return implode('', array_slice($charlist, 0, $i)) . (count($charlist) <= $i ? '' : $end) ;
  57. }
  58. ?>

Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template