当前位置:Gxlcms > PHP教程 > php中文编码判断代码

php中文编码判断代码

时间:2021-07-01 10:21:17 帮助过:11人阅读

  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. }

人气教程排行