PHP Chinese encoding judgment code

WBOY
Release: 2016-07-25 08:53:53
Original
711 people have browsed it
  1. preg_replace(”/([x80-xff])/”,””,$str);
  2. preg_replace(”/([u4e00-u9fa5])/”,””,$str);
Copy code

Example, PHP Chinese encoding judgment.

  1. //Determine whether there is Chinese in the content-gbk (php)
  2. function check_is_chinese($s){
  3. return preg_match('/[x80-xff]./', $s) ;
  4. }
  5. //Get 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. //Intercept string string-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. //Statistical string length-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. / /Intercept 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 > 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; 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. //Determine whether there are Chinese characters-gbk (javascript )
  75. function check_chinese_char(s){
  76. return (s.length != s.replace(/[^x00-xff]/g,"**").length);
  77. }
Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!