douhe4608 2017-04-19 09:45
浏览 97

如何将unicode字符串转换为字符

I want to convert unicode to character.

For this I tried https://stackoverflow.com/a/7278961/4073217 post but it is not working for me.

For Example :

$string = '%u0391%u03b8%u03b1%u03bd%u03b1%u03c3%u03af%u03bf%u03c5';
$string = preg_replace('/%u([0-9A-F]+)/', '&#x$1;', $string);
echo html_entity_decode($string, ENT_COMPAT, 'UTF-8');

Output should be Αθανασίου but above method returning Αb8b1bdb1c3afbfc5.

Am I doing anything wrong? How do I get correct characters from Unicode in php?

  • 写回答

2条回答 默认 最新

  • duangou1953 2017-04-19 10:21
    关注

    the regex:

    $string = preg_replace('/%u([0-9A-F]+)/', '&#x$1;', $string) has A-F which means it will only compare capital A-F characters after 0-9. Since you have all small characters match is failing. Try:

    $string = preg_replace('/%u([0-9a-f]+)/', '&#x$1;', $string);
    

    instead.

    Also check if browser output is utf-8. if not you can use header:

    header('Content-type: text/html; charset=utf-8');
    

    before echoing output

    评论

报告相同问题?

悬赏问题

  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类