PHP Chinese string interception function

WBOY
Release: 2016-07-25 09:11:13
Original
746 people have browsed it
  1. /****First of all, it is the boss of CSDN Forum PHP xuzuning (nagging), which supports three encodings: gb2312, gbk, and big.
  2. Here is the code: ***/
  3. $len = 19;
  4. $text = "How to display only the first few words of a long news title and replace it with...?";
  5. echo strlen($text)<=$len ? $text : (substr($text,0,$len).chr(0)."....");
  6. /****chr(0) is not null
  7. null means nothing, and the value of chr(0) is 0. Expressed in hexadecimal it is 0x00, expressed in binary it is 00000000
  8. Although chr(0) will not display anything, it is a character.
  9. When a Chinese character is truncated, according to the encoding rules, it always has to pull in other characters behind it and interpret them as Chinese characters. This is the reason why garbled characters appear. The combination of values ​​0x81 to 0xff and 0x00 is always displayed as "empty"
  10. According to this feature, adding a chr (0) after the result of substr can prevent garbled characters
  11. Note:
  12. Encode the first byte Second byte
  13. gb2312 0xa1-0xf7 0xa1-0xfe
  14. gbk 0x81-0xfe 0x81-0xfe 0x40-0x7e
  15. big5 0xa1-0xf7 0x81-0xfe 0x40-0x7e
  16. Secondly, this was searched online and supports utf-8 encoding. The original author Unknown:
  17. *****/
  18. function subString_UTF8($str, $start, $lenth)
  19. {
  20. $len = strlen($str);
  21. $r = array();
  22. $n = 0;
  23. $m = 0;
  24. for($i = 0; $i < $len; $i++) {
  25. $x = substr($str, $i, 1);
  26. $a = base_convert(ord($x), 10, 2);
  27. $a = substr ('00000000'.$a, -8);
  28. if ($n < $start){
  29. if (substr($a, 0, 1) == 0) {
  30. }elseif (substr($a, 0 , 3) == 110) {
  31. $i += 1;
  32. }elseif (substr($a, 0, 4) == 1110) {
  33. $i += 2;
  34. }
  35. $n++;
  36. }else{
  37. if (substr($a, 0, 1) == 0) {
  38. $r[ ] = substr($str, $i, 1);
  39. }elseif (substr($a, 0, 3) == 110 ) {
  40. $r[ ] = substr($str, $i, 2);
  41. $i += 1;
  42. }elseif (substr($a, 0, 4) == 1110) {
  43. $r[ ] = substr($str, $i, 3);
  44. $i += 2;
  45. }else{
  46. $r[ ] = '';
  47. }
  48. if (++$m >= $lenth){
  49. break;
  50. }
  51. }
  52. }
  53. return $r;
  54. } // End subString_UTF8;
  55. }// End String
  56. #Since this function returns an array, the join function must be used to display the string:
  57. #join( '',subString_UTF8($str, $start, $lenth));
  58. #You can also append a "..." after this statement when the page is displayed
Copy code


Related labels:
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