简体   繁体   English

如何使用 PHP 将 UTF-8 编码为 Unicode 转义序列,如“\Á”?

[英]How to encode UTF-8 to Unicode escape sequences like "\u00C1" using PHP?

How to encode UTF-8 to Unicode escape sequences like "\Á" using PHP?如何使用 PHP 将 UTF-8 编码为 Unicode 转义序列,如“\Á”?

I found several posts about the opposite.我发现了几个关于相反的帖子。

What I am trying to do is to convert something like Á to \Á to use with Google Charts and some JavaScript alerts.我想要做的是将 Á 之类的内容转换为 \Á 以与 Google Charts 和一些 JavaScript 警报一起使用。

At this time, I am using a function containing several replaces like $str = str_replace("Á","\Á",$str);此时,我正在使用一个包含多个替换的函数,例如$str = str_replace("Á","\Á",$str); . .

Thank you.谢谢你。

json_encode can do that for you. json_encode可以为您做到这一点。

json_encode('Á') will get you "\Á" , so all you would have to do is strip of the doubles quotes from the beginning and end afterwards. json_encode('Á')会给你"\Á" ,所以你所要做的就是从开头和结尾"\Á"双引号。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM