欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2024-01-18 23:28:16
...
  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. }
复制代码