Chinese interception without garbled (2 methods)

Source: Internet
Author: User
Chinese interception without garbled (2 methods)
  1. $str the string to intercept
  2. $len number of characters intercepted
  3. $chars number of characters that have been intercepted
  4. $res a saved string
  5. $chars Save the number of strings that have been intercepted
  6. Offset of $offset intercept
  7. The number of bytes $length string
  8. If $len> $str the number of characters, resulting in a meaningless while loop ($offset < $length limit)
  9. function Utf8sub ($STR, $len) {
  10. if ($len <=0) {
  11. return;
  12. }
  13. $res = "";
  14. $offset = 0;
  15. $chars = 0;
  16. $length =strlen ($STR);
  17. while ($chars < $len && $offset < $length) {
  18. $hign =decbin (Ord (substr ($str, $offset, 1)));
  19. if (strlen ($hign) <8) {
  20. $count = 1;
  21. }elseif (substr ($hign, 0, 3) = = "110") {
  22. $count = 2;
  23. }elseif (substr ($hign, 0,4) = = "1110") {
  24. $count = 3;
  25. }elseif (substr ($hign, 0,5) = = "11110") {
  26. $count = 4;
  27. }elseif (substr ($hign, 0,6) = = "111110") {
  28. $count = 5;
  29. }elseif (substr ($hign, 0,7) = = "1111110") {
  30. $count = 6;
  31. }
  32. $res. =substr ($str, $offset, $count);
  33. $offset + = $count;
  34. $chars +=1;
  35. }
  36. return $res;
  37. }
  38. function Utf8sub1 ($STR, $len) {
  39. $chars = 0;
  40. $res = "";
  41. $offset = 0;
  42. $length =strlen ($STR);
  43. while ($chars < $len && $offset < $length) {
  44. $hign =decbin (Ord (substr ($str, $offset, 1)));
  45. if (strlen ($hign) <8) {
  46. $count = 1;
  47. }elseif ($hign & "11100000" = = "11000000") {
  48. $count = 2;
  49. }elseif ($hign & "11110000" = = "11100000") {
  50. $count = 3;
  51. }elseif ($hign & "11111000" = = "11110000") {
  52. $count = 4;
  53. }elseif ($hign & "11111100" = = "11111000") {
  54. $count = 5;
  55. }elseif ($hign & "11111110" = = "11111100") {
  56. $count = 6;
  57. }
  58. $res. =substr ($str, $offset, $count);
  59. $chars + +;
  60. $offset + = $count;
  61. }
  62. return $res;
  63. }
  64. $a = "Chinese ah people hdj";
  65. Echo utf8sub ($a, 5);
  66. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.