Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How you encode strings like \u00d6?

Tags:

php

decode

This is encoded: \u00d6
This is decoded: Ö

What function I have to use to decode that string into something readable?

\u00d6asdf -> Öasdf
like image 438
ilhan Avatar asked Jul 22 '10 12:07

ilhan


People also ask

How do I encode strings to UTF-8?

In order to convert a String into UTF-8, we use the getBytes() method in Java. The getBytes() method encodes a String into a sequence of bytes and returns a byte array. where charsetName is the specific charset by which the String is encoded into an array of bytes.

Is UTF-8 and Unicode the same?

The Difference Between Unicode and UTF-8Unicode is a character set. UTF-8 is encoding. Unicode is a list of characters with unique decimal numbers (code points).

Is UTF-8 a string?

UTF-8 encodes a character into a binary string of one, two, three, or four bytes. UTF-16 encodes a Unicode character into a string of either two or four bytes. This distinction is evident from their names.


1 Answers

To convert to UTF-8, do:

preg_replace('/\\\\u([0-9a-f]{4})/ie',
    'mb_convert_encoding("&#x$1;", "UTF-8", "HTML-ENTITIES")',
    $string);

Since this is the escaping used in JSON, another option would be json_decode. This would, however, also require escaping double quotes and backslashes before (except those of the \uXXXX escape sequences) and adding double quotes around the string. If, however, the string is indeed JSON-encoded and that's what originally motivated the question, the correct answer would naturally be use json_decode instead of the method above.

like image 76
Artefacto Avatar answered Sep 19 '22 06:09

Artefacto