PHP分割汉字为数组



<?php
$string ="帅朱好cc变c态啊,2!";
$arr1 = str_split_utf8($string,'gbk');
var_dump($arr1);

/**
* 将汉字字符串分割为数组
*
* @param string $str
* @param string $charset 字符编码 默认gbk
* @return Array
*/
function str_split_utf8($str,$charset='gbk') {
$str = iconv($charset,'utf-8',$str);
$split=1;
$array = array();
for ( $i=0; $i < strlen( $str ); ){
$value = ord($str[$i]);
if($value > 127){
if($value >= 192 &amp;&amp; $value <= 223)
$split=2;
elseif($value >= 224 &amp;&amp; $value <= 239)
$split=3;
elseif($value >= 240 &amp;&amp; $value <= 247)
$split=4;
}else{
$split=1;
}
$key = NULL;
for ( $j = 0; $j < $split; $j++, $i++ ) {
$key .= $str[$i];
}
array_push( $array, $key );
}

foreach($array as $key=>$value)
{
$array[$key] = iconv('utf-8',$charset,$value);
}
return $array;
}


 

结果:


( ! ) Warning: Missing argument 1 for cwppos_show_review(), called in /data/www/enjoyphp/wp-content/themes/flat/content-single.php on line 29 and defined in /data/www/enjoyphp/wp-content/plugins/wp-product-review/includes/legacy.php on line 18
Call Stack
#TimeMemoryFunctionLocation
10.0000355192{main}( ).../index.php:0
20.0001355480require( '/data/www/enjoyphp/wp-blog-header.php' ).../index.php:17
30.168010160160require_once( '/data/www/enjoyphp/wp-includes/template-loader.php' ).../wp-blog-header.php:19
40.169210231304include( '/data/www/enjoyphp/wp-content/themes/flat/single.php' ).../template-loader.php:74
50.249310804328get_template_part( ).../single.php:5
60.249310804848locate_template( ).../general-template.php:167
70.249310804944load_template( ).../template.php:643
80.249310805328require( '/data/www/enjoyphp/wp-content/themes/flat/content-single.php' ).../template.php:686
90.258610798288cwppos_show_review( ).../content-single.php:29

发表评论

电子邮件地址不会被公开。 必填项已用*标注