PHP显示utf8字符的奇怪字符

时间:2014-09-11 04:05:16

标签: php wordpress utf-8

我使用以下方式从我的数据库收到了内容:

$strip_title = html_entity_decode(strip_tags($title),ENT_QUOTES,'UTF-8');

echo之后使用此字符串正确显示

[MV HD] Nợ Ai Đó Cả Thế Giới – Phạm Quỳnh Anh

但是当我使用str_split函数显示上面的字符串作为字符数组时:

$result = str_split($title);
echo "<pre>";
print_r($result);
echo "</pre>";

然后结果看起来像这样:

Array
(
    [0] => [
    [1] => M
    [2] => V
    [3] =>  
    [4] => H
    [5] => D
    [6] => ]
    [7] =>  
    [8] => N
    [9] => �
    [10] => �
    [11] => �
......................

我在这里做错了什么?如何解决这个问题?

您可以查看 sandbox demo

1 个答案:

答案 0 :(得分:1)

// this function is get from http://php.net/manual/en/function.str-split.php
function str_split_unicode($str, $l = 0) {
    if ($l > 0) {
        $ret = array();
        $len = mb_strlen($str, "UTF-8");
        for ($i = 0; $i < $len; $i += $l) {
            $ret[] = mb_substr($str, $i, $l, "UTF-8");
        }
        return $ret;
    }
    return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
}

$title = "[MV HD] Nợ Ai Đó Cả Thế Giới - Phạm Quỳnh Anh";
$result = str_split_unicode($title);
echo "<pre>";
print_r($result);
echo "</pre>";

Sandbox Demo