Home > php教程 > php手册 > 字符串按长度分割成数组函数

字符串按长度分割成数组函数

WBOY
Release: 2016-06-07 11:45:42
Original
1453 people have browsed it

支持中文字符串
一个合适的unicode字符串分割函数/**<br>  * 将unicode字符串按传入长度分割成数组<br>  * @param  string  $str 传入字符串<br>  * @param  integer $l   字符串长度<br>  * @return mixed      数组或false<br>  */<br> function str_split_unicode($str, $l = 0) {<br>     if ($l > 0) {<br>         $ret = array();<br>         $len = mb_strlen($str, "UTF-8");<br>         for ($i = 0; $i              $ret[] = mb_substr($str, $i, $l, "UTF-8");<br>         }<br>         return $ret;<br>     }<br>     return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);<br>  }例子:<?php <br /> function str_split_unicode($str, $l = 0) {<br>      if ($l > 0) {<br>          $ret = array();<br>          $len = mb_strlen($str, "UTF-8");<br>          for ($i = 0; $i               $ret[] = mb_substr($str, $i, $l, "UTF-8");<br>          }<br>          return $ret;<br>      }<br>      return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);<br>  }<br> ?><br> <br>  $s = "Il?k süt"; // Mild milk<br> <br>  print_r(str_split($s, 3));<br>  print_r(str_split_unicode($s, 3));<br> <br>  Array<br>  (<br>      [0] => Il?<br>      [1] => ?k <br>      [2] => sü<br>      [3] => t<br>  )<br> <br>  Array<br>  (<br>      [0] => Il?<br>      [1] => k s<br>      [2] => üt<br>  ) 

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template