PHP は UTF8 または GBK でエンコードされた中国語と英語の文字列をインターセプトします

WBOY
リリース: 2016-07-25 08:53:51
オリジナル
853 人が閲覧しました
  1. //String interception

  2. $a = "s@@Hello";
  3. var_dump(strlen_weibo($a,'utf-8'));
  4. 結果 出力は 8 で、s が 1、全角 @ が 2、半角 @ が 1、漢字 2 文字が 4 と数えられます。ソースコードは以下の通りです:

  5. //文字列をインターセプトする関数コード

  6. function strlen_weibo($string, $charset='utf-8')
  7. {
  8. $n = $count = 0 ;
  9. $ length = strlen($string);
  10. if (strto lower($charset) == 'utf-8')
  11. {
  12. while ($n < $length)
  13. {
  14. $currentByte = ord($string[ $n] );
  15. if ($currentByte == 9 ||
  16. $currentByte == 10 ||
  17. (32 <= $currentByte && $currentByte <= 126)) // bbs.it-home.org
  18. {
  19. $ n++;
  20. $count++;
  21. } elseif (194 <= $currentByte && $currentByte <= 223)
  22. {
  23. $n += 2;
  24. $count += 2;
  25. } elseif (224 <; = $currentByte && $currentByte <= 239)
  26. {
  27. $n += 3;
  28. $count += 2;
  29. } elseif (240 <= $currentByte && $currentByte <= 247)
  30. {
  31. $n += 4 ;
  32. $count += 2;
  33. } elseif (248 <= $currentByte && $currentByte <= 251)
  34. {
  35. $n += 5;
  36. $count += 2;
  37. } elseif ($ currentByte == 252 || $currentByte == 253)
  38. {
  39. $n += 6;
  40. $count += 2;
  41. } else
  42. {
  43. $n++;
  44. $count++;
  45. }
  46. if ($count > = $length )
  47. {
  48. Break;
  49. }
  50. }
  51. return $count;
  52. } else
  53. {
  54. for ($i = 0; $i < $length; $i++)
  55. {
  56. if (ord($string) [$i ]) > 127)
  57. {
  58. $i++;
  59. $count++;
  60. }
  61. $count++;
  62. }
  63. return $count;
  64. }
  65. }

コードをコピー


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