支持中文字符串
一个合适的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元