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

WBOY
Lepaskan: 2016-06-13 13:29:27
asal
1126 orang telah melayarinya

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

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
Salin selepas log masuk


------解决方案--------------------
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>
Salin selepas log masuk
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>
Salin selepas log masuk
PHP code


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

PHP code


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




array(4) {
[0] =>
string(2) "大"
[1] =>
string(2) "中"
[2] =>
string(2) "华"
[3] =>
string(2) "区"
}
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan