Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在传递unicode字符时丢失unicode_Php - Fatal编程技术网

Php 在传递unicode字符时丢失unicode

Php 在传递unicode字符时丢失unicode,php,Php,我想以数组对的形式存储unicode字符(字符,字符的实例数),但在传递unicode字符时遇到一些问题: function mb_str_split( $string ) { # Split at all position not after the start: ^ # and not before the end: $ return preg_split('/(?<!^)(?!$)/u', $string ); } 此代码: $str = 'ółś';

我想以数组对的形式存储unicode字符(字符,字符的实例数),但在传递unicode字符时遇到一些问题:

function mb_str_split( $string ) {
    # Split at all position not after the start: ^
    # and not before the end: $
    return preg_split('/(?<!^)(?!$)/u', $string );
} 
此代码:

$str = 'ółś';

var_dump(str_split($str));
显示:

array(6) {
  [0]=>
  string(1) "�"
  [1]=>
  string(1) "�"
  [2]=>
  string(1) "�"
  [3]=>
  string(1) "�"
  [4]=>
  string(1) "�"
  [5]=>
  string(1) "�"
}
我不明白怎么了

但是这个代码:

$arr = array('ó' => 1, 'ł' => 1, 'ś' => 1);
var_dump($arr);
显示:

array(3) {
  ["ó"]=>
  int(1)
  ["ł"]=>
  int(1)
  ["ś"]=>
  int(1)
}
对我来说,这是一个结果,我想实现

编辑:

替换unicode字符的str_split(长度=1):

function mb_str_split( $string ) {
    # Split at all position not after the start: ^
    # and not before the end: $
    return preg_split('/(?<!^)(?!$)/u', $string );
} 
函数mb_str_split($string){
#在所有位置拆分,而不是在启动后:^
#而不是在结束之前:$

返回preg_split('/(?PHP字符串函数,如处理字节,而不是字符

当使用UTF-8时,每个字符可能需要多个字节来表示——这意味着PHP字符串函数并不总是有效的


要在UTF-8中操作字符串,应该使用扩展名,它提供了操作多字节字符串的函数

在您的情况下,我不确定应该使用mbstring的哪个函数;也许?
它的作用似乎不止于str_split()
,但可能会有所帮助……

插入替换(支持$split_length参数):

函数mb_str_split($string,$split_length=-1) { 如果($split_length==-1){ $split_length=1; } 对于($i=0,$len=mb_strlen($string,'UTF-8');$i<$len;$i+=$split_length){ $array[]=mb_substr($string,$i,$split_length,'UTF-8'); } 返回$array; }
我在我的案例中找到了str_split的替代品。谢谢。不客气:-)玩得开心!记住mbstring函数:你会比你想象的更需要它们;-)