文字化けのない中国語傍受(2つの方法)

WBOY
リリース: 2016-07-25 09:01:16
オリジナル
1014 人が閲覧しました
文字化けのない中国語傍受(2つの方法)
  1. //$str インターセプトされる文字列
  2. //$len インターセプトされる文字数
  3. //$chars インターセプトされた文字数
  4. //$res 保存されたstring
  5. // $chars はインターセプトされた文字列の数を格納します
  6. //$offset はインターセプトされたオフセットです
  7. //$length は文字列のバイト数です
  8. //$len>$str の文字数の場合、不要な while ループが発生します。 ($offsetfunction utf8sub($str,$len){
  9. if($len return ;
  10. }
  11. $res="";
  12. $ offset=0;
  13. $ chars=0;
  14. $length=strlen($str);
  15. while($chars
  16. $hign=decbin(ord(substr($str, $offset,1)) );
  17. if(strlen($hign) $count=1;
  18. }elseif(substr($hign,0,3)=="110"){
  19. $count =2;
  20. }elseif (substr($hign,0,4)=="1110"){
  21. $count=3;
  22. }elseif(substr($hign,0,5)=="11110"){
  23. $count=4;
  24. }elseif(substr($hign,0,6)=="111110"){
  25. $count=5;
  26. }elseif(substr($hign,0,7)=="1111110") {
  27. $count=6 ;
  28. }
  29. $res.=substr($str,$offset,$count);
  30. $offset+=$count;
  31. $chars+=1;
  32. }
  33. return $res;
  34. }
  35. function utf8sub1($ str,$len){
  36. $chars=0;
  37. $res="";
  38. $offset=0;
  39. $length=strlen($str);
  40. while($chars<$len && $offset<$length) {
  41. $hign=decbin(ord(substr($str,$offset,1)));
  42. if(strlen($hign)<8){
  43. $count=1;
  44. }elseif ($hign & "11100000 "=="11000000"){
  45. $count=2;
  46. }elseif($hign & "11110000"=="11100000"){
  47. $count=3;
  48. }elseif($hign & "11111000"==" 11110000"){
  49. $count=4;
  50. }elseif($hign & "11111100"=="11111000"){
  51. $count=5;
  52. }elseif($hign & "11111110"= ="11111100"){
  53. $count=6;
  54. }
  55. $res.=substr($str,$offset,$count);
  56. $chars++;
  57. $offset+=$count;
  58. }
  59. return $res;
  60. }
  61. $a="中华ah人hdj";
  62. echo utf8sub($a,5);
  63. ?>
コードをコピー


ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!