Maison >développement back-end >tutoriel php > 怎么将中文字符串分割成单个字符
【求助】 如何将中文字符串分割成单个字符
<!-- 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 /**************************** * 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>
大 中 华 区 [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>
$county="大中华区"; $array=str_split($county,2); <br><font color="#e78608">------解决方案--------------------</font><br>