Kristaps J.
Kristaps J.

Reputation: 352

UTF-8 Hex code to Unicode

Lets say I have string in Russian cyrillics "журнал", which has following hex code:

d0 b6 d1 83 d1 80 d0 bd d0 b0 d0 bb

But how can I convert this hex code back to human readable cyrillics string in PHP? I have checked so many sources, nothing have worked so far.

Upvotes: 1

Views: 354

Answers (1)

Mihai Matei
Mihai Matei

Reputation: 24276

You can use this:

function hex2str($hex) {
    $str = '';
    for($i = 0; $i < strlen($hex); $i += 2) {
        $str .= chr(hexdec(substr($hex, $i, 2)));
    }
    return $str;
}

$string = preg_replace('/\s+/', '', 'd0 b6 d1 83 d1 80 d0 bd d0 b0 d0 bb');

echo hex2str($string);

Upvotes: 1

Related Questions