Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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中循环显示字符集?_Php - Fatal编程技术网

如何在php中循环显示字符集?

如何在php中循环显示字符集?,php,Php,测试此代码时,它将只回显97 我想用while循环来循环show979899255521421734,我该怎么做 <?php $utf8Character = 'abc提r哦'; list(, $ord) = unpack('N', mb_convert_encoding($utf8Character, 'UCS-4BE', 'UTF-8')); echo $ord; ?> 如何使用while循环显示97 98 99 25552 114 21734?您尝试迭代字符失败,因为strl

测试此代码时,它将只回显
97
我想用while循环来循环show
979899255521421734
,我该怎么做

<?php
$utf8Character = 'abc提r哦';
list(, $ord) = unpack('N', mb_convert_encoding($utf8Character, 'UCS-4BE', 'UTF-8'));
echo $ord;
?>

如何使用while循环显示
97 98 99 25552 114 21734

您尝试迭代字符失败,因为
strlen()
实际计算字符串的字节数。因此,如果您使用
$utf8Character[$i]
,您将永远无法获得整个汉字,因为它由UTF-8中的三个字节组成。相反,您将只获得一个字节

为了修复它,您可以使用
mb_strlen()
计算多字节设置中的字符数。这将考虑编码,如果是字符串,将返回
6


您尝试对字符进行迭代失败,因为
strlen()
实际上计算字符串的字节数。因此,如果您使用
$utf8Character[$i]
,您将永远无法获得整个汉字,因为它由UTF-8中的三个字节组成。相反,您将只获得一个字节

为了修复它,您可以使用
mb_strlen()
计算多字节设置中的字符数。这将考虑编码,如果是字符串,将返回
6


您发布的第一段代码中有几个错误。 让我们尝试在下面确定并修复它们:

$utf8 = 'abc提r哦';
// Put the UCS string into a variable for easier handling
$ucs  = mb_convert_encoding($utf8, 'UCS-4BE', 'UTF-8');
echo ($ucs."\n");

// Error #1: 'N' unpacks a single 32-bit integer
print_r(unpack('N', $ucs));
// It prints:
//    Array
//    (
//        [1] => 97
//    )

// Fix: 'N*' unpacks as many 32-bit integers it can find in the input
print_r(unpack('N*', $ucs));
// It prints:
//    Array
//    (
//        [1] => 97
//        [2] => 98
//        [3] => 99
//        [4] => 25552
//        [5] => 114
//        [6] => 21734
//    )

// Error #2: list(, $ord) ignores everything starting with index 2
list(, $ord) = unpack('N*', $ucs);
var_dump($ord);
// It prints:
//     int(97)
可从上述代码中提取解决方案:

$utf8 = 'abc提r哦';
$ucs  = mb_convert_encoding($utf8, 'UCS-4BE', 'UTF-8');

foreach (unpack('N*', $ucs) as $ord) {
    echo($ord."\n");
}

您发布的第一段代码中有几个错误。 让我们尝试在下面确定并修复它们:

$utf8 = 'abc提r哦';
// Put the UCS string into a variable for easier handling
$ucs  = mb_convert_encoding($utf8, 'UCS-4BE', 'UTF-8');
echo ($ucs."\n");

// Error #1: 'N' unpacks a single 32-bit integer
print_r(unpack('N', $ucs));
// It prints:
//    Array
//    (
//        [1] => 97
//    )

// Fix: 'N*' unpacks as many 32-bit integers it can find in the input
print_r(unpack('N*', $ucs));
// It prints:
//    Array
//    (
//        [1] => 97
//        [2] => 98
//        [3] => 99
//        [4] => 25552
//        [5] => 114
//        [6] => 21734
//    )

// Error #2: list(, $ord) ignores everything starting with index 2
list(, $ord) = unpack('N*', $ucs);
var_dump($ord);
// It prints:
//     int(97)
可从上述代码中提取解决方案:

$utf8 = 'abc提r哦';
$ucs  = mb_convert_encoding($utf8, 'UCS-4BE', 'UTF-8');

foreach (unpack('N*', $ucs) as $ord) {
    echo($ord."\n");
}

您正在使用的文档的编码是什么?UCS-4BE?我使用编码UTF-8您可以尝试使用
mb\u strlen()
。您使用的文档的编码是什么?UCS-4BE?我使用编码UTF-8,您可以尝试使用
mb\u strlen()