特殊字符无法正确输出

时间:2016-05-11 11:28:05

标签: php

我有这段代码:

$line = 'ABBEKÅS';
echo 'word is '.$line.PHP_EOL;
for($i=0;$i<strlen($line);$i++){
    echo 'char '.$line[$i].PHP_EOL;
}

结果是:

word is ABBEKÅS
char A
char B
char B
char E
char K
char ?
char ?
char S

为什么这个单词看起来不错而单个字符不是? 我试图将两者都转换为utf-8和ISO都没有帮助。这就是我试过的:

$line = iconv("UTF-8", "ISO-8859-1", $line);

$line = iconv("ISO-8859-1", "UTF-8", $line);

结果仍然相同。

2 个答案:

答案 0 :(得分:1)

Because you're assuming that each character is only a single byte in your for loop (you're iterating a byte at a time, not a character at a time), and your Å is a multi-byte character.

If you need to break multi-byte strings into segments/characters, use PHP's multibyte string functions

for($i=0;$i<mb_strlen($line);$i++){
    echo 'char '.mb_substr($line, $i, 1).PHP_EOL;
}

Demo

答案 1 :(得分:0)

So the solution is to retrieve the character with mb_substr. Like this:

$line = 'ABBEKÅS';
echo 'word is '.$line.PHP_EOL;
for($i=0;$i<strlen($line);$i++){
    echo mb_substr($line, $i, 1).PHP_EOL;
}