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:

Read More
[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

Related posts

Leave a Reply

1 comment

  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.