首页 > 网站 > 建站经验 > 正文

支持中文的PHP按字符、串长度分割成数组代码

2019-11-02 14:43:04
字体:
来源:转载
供稿:网友

   自己编写的PHP按字符串长度分割成数组代码,支持中文字符,下面给出代码和使用方法,有需要的小伙伴可以参考下。

  以下是我编写的代码,实现对中英文混杂字符进行分割:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?php function mbStrSplit ($string, $len=1) { $start = 0; $strlen = mb_strlen($string); while ($strlen) { $array[] = mb_substr($string,$start,$len,"utf8"); $string = mb_substr($string, $len, $strlen,"utf8"); $strlen = mb_strlen($string); } return $array; }   header('Content-type:text/html;charset=utf-8'); $str = '我爱北京3我爱上海-我爱xianggang'; $r = mbStrSplit($str, 4); echo '<pre>'; print_r($r); echo '<
搞笑内涵图[www.62-6.com/1/nahantu/]
;/pre>'; ?>

  运行结果:

  ?

1 2 3 4 5 6 7 8 9 Array ( [0] => 我爱北京 [1] => 3我爱上 [2] => 海-我爱 [3] => xian [4] => ggan [5] => g )

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <?php function str_split_unicode($str, $l = 0) { if ($l > 0) { $ret = array(); $len = mb_strlen($str,"UTF-8"); for ($i = 0; $i < $len; $i += $l) { $ret[] = mb_substr($str, $i, $l,"UTF-8"); } return $ret; } return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY); } ?> $s ="还未如愿sss见gfg者不rtret不朽"; // Mild milk print_r(str_split($s, 5)); print_r(str_split_unicode($s, 5));   //输出 //Array ( [0] => 还�� [1] => �如� [2] => ��sss [3] => 见gf [4] => g者� [5] => ��rtr [6] => et不 [7] => 朽 ) //Array ( [0] => 还未如愿s [1] => ss见gf [2] => g者不rt [3] => ret不朽 )
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表