php中文編碼判斷代碼

來源:互聯網
上載者:User
  1. preg_replace(”/([\x80-\xff])/”,”",$str);
  2. preg_replace(”/([u4e00-u9fa5])/”,”",$str);
複製代碼

例子,php中文編碼判斷。

  1. //判斷內容裡有沒有中文-gbk (php)
  2. function check_is_chinese($s){
  3. return preg_match('/[\x80-\xff]./', $s);
  4. }
  5. //擷取字串長度-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. //截取字串字串-gbk (php)
  15. function gb_substr($str, $len){
  16. $count = 0;
  17. for($i=0; $iif($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. //統計字串長度-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 > 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. //截取字串-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 > 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. //判斷是否是有韓文-utf-8 (javascript)
  67. function checkkoreachar(str) {
  68. for(i=0; iif(((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. //判斷是否有中文字元-gbk (javascript)
  75. function check_chinese_char(s){
  76. return (s.length != s.replace(/[^\x00-\xff]/g,"**").length);
  77. }
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.