ホームページ  >  記事  >  バックエンド開発  >  PHP中国語エンコード判定コード

PHP中国語エンコード判定コード

WBOY
WBOYオリジナル
2016-07-25 08:53:53705ブラウズ
  1. preg_replace(”/([x80-xff])/”,””,$str);
  2. preg_replace(”/([u4e00-u9fa5])/”,””,$str);
コードをコピー

例、PHP中国語エンコード判定。

  1. //content-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; $i=0; $i$s = substr($str, $i, 1);
  8. if (preg_match("/[x80-xff]/", $s))
  9. ++$count; return $ count;
  10. }
  11. //インターセプト文字列 string-gbk (php)
  12. function gb_substr($str, $len){
  13. for($i=0; $iif($count == $len) ブレーク;
  14. if(preg_match("/[x80-xff]/", substr($str, $i, 1))) ++$i; ++$count;
  15. }
  16. return substr($str, 0, $i);
  17. //統計文字列長-utf8 (php)
  18. function utf8_strlen($str) {
  19. $count = 0; $i = 0; $i <$str); $i++){
  20. $value = ord($str[$i]);
  21. $count++; $value >= 192 && $value <= 223) $i++;
  22. elseif($value >= 224 && $value <= 239) $i = $i + 2; 240 && $value <= 247) $i = $i + 3;
  23. else die('utf-8 互換文字列ではありません');
  24. $count++
  25. }
  26. /インターセプト string-utf8(php)
  27. function utf8_substr($str,$position,$length){
  28. $start_position = strlen($str)
  29. $end_position =
  30. $count; = 0;
  31. for($i = 0; $i if($count >= $position && $start_position > $i){
  32. $start_position = $i ;
  33. $start_byte = $count;
  34. if(($count-$start_byte)>=$length) {
  35. $end_position = $i;
  36. $value = ord($i ]);
  37. if($値 > 127){
  38. $count++;
  39. if($値 >= 192 && $値 <= 223) $i++($値 >= 224 && $値 < ;= 239) $i = $i + 2;
  40. elseif($value >= 240 && $value else die('utf-8 互換の文字列ではありません' );
  41. }
  42. $count++;
  43. }
  44. return(substr($str,$start_position,$end_position-$start_position));
  45. }
  46. //Korean-utf-8 (javascript) があるかどうかを判断する
  47. function checkkoreachar( str) {
  48. for(i=0; iif(((str.charcodeat(i) > 0x3130 && str.charcodeat(i) return true;
  49. }
  50. }
  51. return false
  52. }
  53. //中国語の文字があるかどうかを判断する -gbk (javascript )
  54. function check_chinese_char(s){
  55. return (s.length != s.replace(/[^x00-xff]/g,"**").length);
  56. }
  57. コードをコピー
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。