Php Chinese encoding judgment code-php Tutorial

Source: Internet
Author: User
Php Chinese encoding judgment code

  1. Preg_replace ("/([\ x80-\ xff])/", "", $ str );
  2. Preg_replace ("/([u4e00-u9fa5])/", ", $ str );

Example: php Chinese encoding judgment.

  1. // Determine whether the content contains Chinese characters-gbk (php)
  2. Function check_is_chinese ($ s ){
  3. Return preg_match ('/[\ x80-\ xff]./', $ s );
  4. }
  5. // Obtain the string length-gbk (php)
  6. Function gb_strlen ($ str ){
  7. $ Count = 0;
  8. For ($ I = 0; $ I $ S = substr ($ str, $ I, 1 );
  9. If (preg_match ("/[\ x80-\ xff]/", $ s) ++ $ I;
  10. + + $ Count;
  11. }
  12. Return $ count;
  13. }
  14. // Truncate the string-gbk (php)
  15. Function gb_substr ($ str, $ len ){
  16. $ Count = 0;
  17. For ($ I = 0; $ I If ($ count = $ len) break;
  18. If (preg_match ("/[\ x80-\ xff]/", substr ($ str, $ I, 1) ++ $ I;
  19. + + $ Count;
  20. }
  21. Return substr ($ str, 0, $ I );
  22. }
  23. // Count the length of the string-utf8 (php)
  24. Function utf8_strlen ($ str ){
  25. $ Count = 0;
  26. For ($ I = 0; $ I <strlen ($ str); $ I ++ ){
  27. $ Value = ord ($ str [$ I]);
  28. If ($ value & gt; 127 ){
  29. $ Count ++;
  30. If ($ value >=192 & $ value <= 223) $ I ++;
  31. Elseif ($ value >=224 & $ value <= 239) $ I = $ I + 2;
  32. Elseif ($ value >=240 & $ value <= 247) $ I = $ I + 3;
  33. Else die ('not a UTF-8 compatible string ');
  34. }
  35. $ Count ++;
  36. }
  37. Return $ count;
  38. }
  39. // Truncate the string-utf8 (php)
  40. Function utf8_substr ($ str, $ position, $ length ){
  41. $ Start_position = strlen ($ str );
  42. $ Start_byte = 0;
  43. $ End_position = strlen ($ str );
  44. $ Count = 0;
  45. For ($ I = 0; $ I <strlen ($ str); $ I ++ ){
  46. If ($ count >=$ position & $ start_position> $ I ){
  47. $ Start_position = $ I;
  48. $ Start_byte = $ count;
  49. }
  50. If ($ count-$ start_byte)> = $ length ){
  51. $ End_position = $ I;
  52. Break;
  53. }
  54. $ Value = ord ($ str [$ I]);
  55. If ($ value & gt; 127 ){
  56. $ Count ++;
  57. If ($ value >=192 & $ value <= 223) $ I ++;
  58. Elseif ($ value >=224 & $ value <= 239) $ I = $ I + 2;
  59. Elseif ($ value >=240 & $ value <= 247) $ I = $ I + 3;
  60. Else die ('not a UTF-8 compatible string ');
  61. }
  62. $ Count ++;
  63. }
  64. Return (substr ($ str, $ start_position, $ end_position-$ start_position ));
  65. }
  66. // Determine whether there is Korean-UTF-8 (javascript)
  67. Function checkkoreachar (str ){
  68. For (I = 0; I If (str. charcodeat (I)> 0x3130 & str. charcodeat (I) <0x318f) | (str. charcodeat (I)> = 0xac00 & str. charcodeat (I) <= 0xd7a3 ))){
  69. Return true;
  70. }
  71. }
  72. Return false;
  73. }
  74. // Determine whether a Chinese character-gbk (javascript) exists)
  75. Function check_chinese_char (s ){
  76. Return (s. length! = S. replace (/[^ \ x00-\ xff]/g, "**"). length );
  77. }

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.