简体   繁体   中英

PHP display weird characters for utf8 characters

I received the content from my database using:

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

it's displayed correctly with this string after echo :

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

but when I display above string as an array of characters using str_split function:

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

then the result look something like this:

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

What am I doing wrong here? How to correct this problem?

You can take a look at this sandbox demo

// 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM