【问题标题】:Converting Unicode character to text in PHP is not working在 PHP 中将 Unicode 字符转换为文本不起作用
【发布时间】:2015-12-01 10:32:49
【问题描述】:

我正在尝试将 Unicode 字符转换为 PHP 中的文本。但字符串是 Unicode 字符和文本的混合体。但它不起作用。

我点击了这个链接 (Unicode character in PHP string)

<?php

   $unicodeChar = "{'singer': u'', 'name': u'\\\\u101c\\\\u1031\\\\u1011\\\\u1032\\\\u101c\\\\u103d\\\\u103e\\\\u1004\\\\u1037\\\\u103a\\\\u101c\\\\u102d\\\\u102f\\\\u1000\\\\u103a'}\\r\\n\\r\\n    artist          : Thar Gyi\\r\\n    album           : Sal Pone Ta Pone\\r\\n    genre           : R&B\\r\\n    copyright       : MyanmarSongs.NET\\r\\n    track           : 1\\r\\n    title           : Lay Htal Hlwint Lite";
   echo json_decode('"'.$unicodeChar.'"');
   echo mb_convert_encoding($unicodeChar, 'UTF-8', 'HTML-ENTITIES'); 
   echo mb_convert_encoding($unicodeChar, 'UTF-8', 'UTF-16BE'); showing nothing

?>

当值是我使用的 Unicode 字符和文本的混合时,上述所有场景都不起作用。但是当值像这样简单时,它就可以工作了:

$unicodeChar = '\u1000';
echo json_decode('"'.$unicodeChar.'"');

我怎样才能做到这一点?

【问题讨论】:

  • 首先输入 $unicodeChar 中的 json 您给出的不是 i json 格式。请给我正确的json。所以我可以检查一下
  • 不,它可以是任何格式。那么请问我该如何检测呢?
  • 在php中使用mime_content_type函数php.net/manual/en/function.mime-content-type.php
  • 我的值来自数据库。不是来自像api这样的地方。那么我怎样才能得到它呢?

标签: php mysql codeigniter unicode


【解决方案1】:

使用以下代码

$unicodeChar = '\u1000';
echo json_decode('"'.$unicodeChar.'"');

【讨论】:

  • 当值如此简单时它就起作用了。我已经更新了问题。
猜你喜欢
  • 2011-05-14
  • 2011-02-03
  • 2020-12-30
  • 2014-11-02
  • 1970-01-01
  • 2019-08-20
  • 2019-04-19
  • 2021-11-27
  • 1970-01-01
相关资源
最近更新 更多