Heim  >  Artikel  >  Backend-Entwicklung  >  php截取utf8或gbk编码中英文字符串

php截取utf8或gbk编码中英文字符串

WBOY
WBOYOriginal
2016-07-25 08:53:51847Durchsuche
  1. //字符串截取

  2. $a = "s@@你好";
  3. var_dump(strlen_weibo($a,'utf-8'));
  4. 结果输出为8,其中字母s计数为1,全角@计数为2,半角@计数为1,两个中文计数为4。源码如下:
  5. //截取字符串的函数代码

  6. function strlen_weibo($string, $charset='utf-8')
  7. {
  8. $n = $count = 0;
  9. $length = strlen($string);
  10. if (strtolower($charset) == 'utf-8')
  11. {
  12. while ($n {
  13. $currentByte = ord($string[$n]);
  14. if ($currentByte == 9 ||
  15. $currentByte == 10 ||
  16. (32 {
  17. $n++;
  18. $count++;
  19. } elseif (194 {
  20. $n += 2;
  21. $count += 2;
  22. } elseif (224 {
  23. $n += 3;
  24. $count += 2;
  25. } elseif (240 {
  26. $n += 4;
  27. $count += 2;
  28. } elseif (248 {
  29. $n += 5;
  30. $count += 2;
  31. } elseif ($currentByte == 252 || $currentByte == 253)
  32. {
  33. $n += 6;
  34. $count += 2;
  35. } else
  36. {
  37. $n++;
  38. $count++;
  39. }
  40. if ($count >= $length)
  41. {
  42. break;
  43. }
  44. }
  45. return $count;
  46. } else
  47. {
  48. for ($i = 0; $i {
  49. if (ord($string[$i]) > 127)
  50. {
  51. $i++;
  52. $count++;
  53. }
  54. $count++;
  55. }
  56. return $count;
  57. }
  58. }
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn