Arthur Gelcer
Arthur Gelcer

Reputation: 69

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.

Upvotes: 0

Views: 979

Answers (1)

C3roe
C3roe

Reputation: 96339

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

Upvotes: 1

Related Questions