首页  >  文章  >  后端开发  >  php中文编码判断代码

php中文编码判断代码

WBOY
WBOY原创
2016-07-25 08:53:53705浏览
  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 $value = ord($str[$i]);
  27. if($value > 127) {
  28. $count++;
  29. if($value >= 192 && $value elseif($value >= 224 && $value elseif($value >= 240 && $value else die('not a utf-8 compatible string');
  30. }
  31. $count++;
  32. }
  33. return $count;
  34. }
  35. //截取字符串-utf8(php)
  36. function utf8_substr($str,$position,$length){
  37. $start_position = strlen($str);
  38. $start_byte = 0;
  39. $end_position = strlen($str);
  40. $count = 0;
  41. for($i = 0; $i if($count >= $position && $start_position > $i){
  42. $start_position = $i;
  43. $start_byte = $count;
  44. }
  45. if(($count-$start_byte)>=$length) {
  46. $end_position = $i;
  47. break;
  48. }
  49. $value = ord($str[$i]);
  50. if($value > 127){
  51. $count++;
  52. if($value >= 192 && $value elseif($value >= 224 && $value elseif($value >= 240 && $value else die('not a utf-8 compatible string');
  53. }
  54. $count++;
  55. }
  56. return(substr($str,$start_position,$end_position-$start_position));
  57. }
  58. //判断是否是有韩文-utf-8 (javascript)
  59. function checkkoreachar(str) {
  60. for(i=0; iif(((str.charcodeat(i) > 0x3130 && str.charcodeat(i) = 0xac00 && str.charcodeat(i) return true;
  61. }
  62. }
  63. return false;
  64. }
  65. //判断是否有中文字符-gbk (javascript)
  66. function check_chinese_char(s){
  67. return (s.length != s.replace(/[^\x00-\xff]/g,"**").length);
  68. }
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn