php PHP函数将unicode转换为特殊字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2577871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:58:48  来源:igfitidea点击:

PHP function to convert unicode to special characters?

phpunicode

提问by rawrrrrrrrr

Is there a php function to handle the encodings below?

是否有一个 php 函数来处理下面的编码?

  .replaceAll("\u00c3\u0080", "À")
  .replaceAll("\u00c3\u0081", "Á")
  .replaceAll("\u00c3\u0082", "Â")
  .replaceAll("\u00c3\u0083", "Ã")
  .replaceAll("\u00c3\u0084", "Ä")
  .replaceAll("\u00c3\u0085", "Å")
  .replaceAll("\u00c3\u0086", "Æ")
  .replaceAll("\u00c3\u00a0", "à")
  .replaceAll("\u00c3\u00a1", "á")
  .replaceAll("\u00c3\u00a2", "â")
  .replaceAll("\u00c3\u00a3", "ã")
  .replaceAll("\u00c3\u00a4", "ä")
  .replaceAll("\u00c3\u00a5", "å")
  .replaceAll("\u00c3\u00a6", "æ")
  .replaceAll("\u00c3\u0087", "Ç")
  .replaceAll("\u00c3\u00a7", "ç")
  .replaceAll("\u00c3\u0090", "Ð")
  .replaceAll("\u00c3\u00b0", "ð")
  .replaceAll("\u00c3\u0088", "È")
  .replaceAll("\u00c3\u0089", "É")
  .replaceAll("\u00c3\u008a", "Ê")
  .replaceAll("\u00c3\u008b", "Ë")
  .replaceAll("\u00c3\u00a8", "è")
  .replaceAll("\u00c3\u00a9", "é")
  .replaceAll("\u00c3\u00aa", "ê")
  .replaceAll("\u00c3\u00ab", "ë")
  .replaceAll("\u00c3\u008c", "Ì")
  .replaceAll("\u00c3\u008d", "Í")
  .replaceAll("\u00c3\u008e", "Î")
  .replaceAll("\u00c3\u008f", "Ï")
  .replaceAll("\u00c3\u00ac", "ì")
  .replaceAll("\u00c3\u00ad", "í")
  .replaceAll("\u00c3\u00ae", "î")
  .replaceAll("\u00c3\u00af", "ï")
  .replaceAll("\u00c3\u0091", "Ñ")
  .replaceAll("\u00c3\u00b1", "ñ")
  .replaceAll("\u00c3\u0092", "Ò")
  .replaceAll("\u00c3\u0093", "Ó")
  .replaceAll("\u00c3\u0094", "Ô")
  .replaceAll("\u00c3\u0095", "Õ")
  .replaceAll("\u00c3\u0096", "Ö")
  .replaceAll("\u00c3\u0098", "Ø")
  .replaceAll("\u00c5\u0092", "Œ")
  .replaceAll("\u00c3\u00b2", "ò")
  .replaceAll("\u00c3\u00b3", "ó")
  .replaceAll("\u00c3\u00b4", "ô")
  .replaceAll("\u00c3\u00b5", "õ")
  .replaceAll("\u00c3\u00b6", "ö")
  .replaceAll("\u00c3\u00b8", "ø")
  .replaceAll("\u00c5\u0093", "œ")
  .replaceAll("\u00c3\u0099", "Ù")
  .replaceAll("\u00c3\u009a", "Ú")
  .replaceAll("\u00c3\u009b", "Û")
  .replaceAll("\u00c3\u009c", "Ü")
  .replaceAll("\u00c3\u00b9", "ù")
  .replaceAll("\u00c3\u00ba", "ú")
  .replaceAll("\u00c3\u00bb", "û")
  .replaceAll("\u00c3\u00bc", "ü")
  .replaceAll("\u00c3\u009d", "Ý")
  .replaceAll("\u00c5\u00b8", "Ÿ")
  .replaceAll("\u00c3\u00bd", "ý")
  .replaceAll("\u00c3\u00bf", "ÿ");

回答by Amber

Try mb_convert_encoding()with the "to" encoding as 'HTML-ENTITIES', and (if necessary) the "from" encoding set to 'UTF-8'or whichever Unicode encoding you're using.

尝试mb_convert_encoding()将“to”编码设为'HTML-ENTITIES',以及(如有必要)将“from”编码设置为'UTF-8'或您正在使用的任何 Unicode 编码。

回答by Vic Seedoubleyew

I found this to be interesting, where the previous answer was not working :

我发现这很有趣,以前的答案不起作用:

function jsonRemoveUnicodeSequences($struct) {
   return preg_replace("/\\u([a-f0-9]{4})/e", "iconv('UCS-4LE','UTF-8',pack('V', hexdec('U')))", json_encode($struct));
}

I found it here : http://www.avoid.org/replace-u-characters-in-json-string/

我在这里找到它:http: //www.avoid.org/replace-u-characters-in-json-string/

回答by raveren

Use strtr, it's blazing fast.

使用strtr,它非常快。