Home > php教程 > php手册 > PHP按字符串长度分割成数组,支持中文

PHP按字符串长度分割成数组,支持中文

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 19:38:23
Original
1952 people have browsed it

PHP按字符串长度分割成数组,支持中文 http://blog.ppsql.net/post-1843.html 无 ?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($st

PHP按字符串长度分割成数组,支持中文
http://blog.ppsql.net/post-1843.html
<?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不朽 )
http://blog.ppsql.net/post-1843.html
Copy after login
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template