0

I'm using an api that returns plain text in UTF8 since the accents appear as code. I've tried several solutions found on the internet and none of them work. I cannot understand why. It already includes the UTF-8 Meta html tag in the header, includes the following code before printing the text: header('Content-Type: text/html; charset=utf-8');

And now I'm doing it like this:

$string = 'Sonhar com um bar indica que voc\u00ea receber\u00e1 um aviso no trabalho que mudar\u00e1 seus planos para o pr\u00f3ximo ano. Voc\u00ea sentir\u00e1 que tudo est\u00e1 fluindo ao seu redor em uma dire\u00e7\u00e3o positiva. Voc\u00ea brinca com as emo\u00e7\u00f5es, mas ter\u00e1 que devolver algo, e n\u00e3o exatamente material. Seria \u00f3timo se voc\u00ea passasse o dia com ela e organizasse uma noite rom\u00e2ntica. Sensa\u00e7\u00e3o, tudo o que entra em voc\u00ea atrav\u00e9s dos sentidos, abre sua mente.';

echo utf8_encode($string);

None works!

How do I resolve this?

  • Try using `"` instead of `'`, and `\u{00ea}` instead of `\u00ea`, and directly using `echo $string;` – qrsngky Dec 02 '22 at 03:13
  • did not work friend – Prinple Vrlo Dec 02 '22 at 03:20
  • Related: [Best way to convert unicode special characters to html while escaping slashes?](https://stackoverflow.com/q/63802302/2943403) and [How to decode Unicode escape sequences like "\u00ed" to proper UTF-8 encoded characters?](https://stackoverflow.com/q/2934563/2943403) – mickmackusa Dec 02 '22 at 03:39
  • Simply `json_decode()` it https://3v4l.org/W2Yp4 – mickmackusa Dec 02 '22 at 03:44

1 Answers1

1

One can replace each escape sequence with the appropriate multibyte character.

<?php
header('Content-Type: text/plain; charset=utf-8');

$string = 'Sonhar com um bar indica que voc\u00ea receber\u00e1 um aviso no trabalho que mudar\u00e1 seus planos para o pr\u00f3ximo ano. Voc\u00ea sentir\u00e1 que tudo est\u00e1 fluindo ao seu redor em uma dire\u00e7\u00e3o positiva. Voc\u00ea brinca com as emo\u00e7\u00f5es, mas ter\u00e1 que devolver algo, e n\u00e3o exatamente material. Seria \u00f3timo se voc\u00ea passasse o dia com ela e organizasse uma noite rom\u00e2ntica. Sensa\u00e7\u00e3o, tudo o que entra em voc\u00ea atrav\u00e9s dos sentidos, abre sua mente.';

$string2 = preg_replace_callback(
    "/\\\\u.{4}/",
    function ($e) {
        $charVal = hexdec($e[0]);
        return mb_chr($charVal);
    },
    $string
);

echo $string2;
?>

Output in a browser:

Sonhar com um bar indica que você receberá um aviso no trabalho que mudará seus planos para o próximo ano. Você sentirá que tudo está fluindo ao seu redor em uma direção positiva. Você brinca com as emoções, mas terá que devolver algo, e não exatamente material. Seria ótimo se você passasse o dia com ela e organizasse uma noite romântica. Sensação, tudo o que entra em você através dos sentidos, abre sua mente.

(I haven't handled the cases where there are invalid Unicode escape sequences, though)

qrsngky
  • 2,263
  • 2
  • 13
  • 10
  • Is this the first question of this kind on Stack Overflow? – mickmackusa Dec 02 '22 at 03:33
  • @mickmackusa now that you mentioned it I found a thread that is very similar, but the accepted answer seems to use a very old version of php: https://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-cha Took quite a number of attempts to search, though (trying various keywords), I didn't succeed in the first few tries. – qrsngky Dec 02 '22 at 03:40
  • We don't need to find a duplicate with a great answer. We need need to find a well-presented question. If your insights are _better_ or even unique, add your insights to the older page and vote to close the new question. – mickmackusa Dec 02 '22 at 03:45