Skip to content
Advertisement

How to encode UTF-8 to Unicode escape sequences like “u00C1” using PHP?

How to encode UTF-8 to Unicode escape sequences like “u00C1” using PHP?

I found several posts about the opposite.

What I am trying to do is to convert something like Á to u00C1 to use with Google Charts and some JavaScript alerts.

At this time, I am using a function containing several replaces like $str = str_replace("Á","u00C1",$str);.

Thank you.

Advertisement

Answer

json_encode can do that for you.

json_encode('Á') will get you "u00c1", so all you would have to do is strip of the doubles quotes from the beginning and end afterwards.

You can directly use it on text strings that contain such special characters in some places:

echo trim(json_encode('Foo Á Bar'), '"');

// result: Foo u00c1 Bar
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement