Home  >  Article  >  Backend Development  >  怎么将中文字符串分割成单个字符

怎么将中文字符串分割成单个字符

WBOY
WBOYOriginal
2016-06-13 13:29:271128browse

【求助】 如何将中文字符串分割成单个字符

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
 $county="大中华区";
 
 echo "<font color="red">".$ci=iconv_strlen($county,"UTF-8")."</font>";
         for($j=0;$j


------解决方案--------------------
PHP code

<?php /****************************
 * subCNchar() 截取汉字 *
 * [$str]     [要截取的字符串]
 * [$start]   [截取的起始位置]
 * [$length]  [要截取的长度]
 * [$charset] [字符串编码]
 ****************************/
function subCNchar($str, $start = 0, $length, $charset = "utf-8") {
    if (strlen($str) <= $length)
        return $str;

    $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
    $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
    $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
    $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
    preg_match_all($re[$charset], $str, $match);

    $slice = join("", array_slice($match[0], $start, $length));

    return $slice;
}
$county="大中华区";
$res=subCNchar($county,0,1);
echo $res;
#大 
?>
<br><font color="#e78608">------解决方案--------------------</font><br>
PHP code
大
中
华
区
[User:root Time:19:21:36 Path:/home/liangdong/php]$ cat mb.php 
<?php /* file-encoding : utf-8*/
/* export LANG=zh_CN.utf-8*/

$str = "大中华区";
$len = mb_strlen($str, "utf-8");
for ($i = 0; $i != $len; ++ $i) {
        $letter = mb_substr($str, $i, 1, "utf-8");
        echo $letter . PHP_EOL;
}
?>
<br><font color="#e78608">------解决方案--------------------</font><br>
PHP code


 $county="大中华区";
$array=str_split($county,2);
<br><font color="#e78608">------解决方案--------------------</font><br>
探讨

PHP code


$county="大中华区";
$array=str_split($county,2);




array(4) {
[0] =>
string(2) "大"
[1] =>
string(2) "中"
[2] =>
string(2) "华"
[3] =>
string(2) "区"
}
Statement:
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