日期:2014-05-17  浏览次数:20532 次

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

 $county="大中华区";
 
 echo "<font color='red'>".$ci=iconv_strlen($county,"UTF-8")."</font>";
         for($j=0;$j<$ci;$j++){
            echo  $e.=iconv_substr($county,$j,$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;
#大 
?>

------解决方案--------------------
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;
}
?>

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


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

------解决方案--------------------
探讨

PHP code


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




array(4) {
[0] =>
string(2) "大"
[1] =>
string(2) "中"
[2] =>
string(2) "华"
[3] =>
string(2) "区"
}