在网页上显示之前将unicode转换为普通字符

时间:2011-01-08 16:19:23

标签: php html unicode

我正在使用openinviter类从电子邮件中导入联系人。但是,它显示非英语字符的Unicodes(例如,波兰语),例如u0117(和其他代码的类似类型)而不是普通字符。无论如何,我可以将unicodes转换为字符然后显示它们吗?

以下是该页面的原始代码: http://pastebin.com/d0tkpxbv

感谢。

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

使用preg_replace()将值转换为十六进制数字实体:

<?php
$text = 'Wladisu0142aw';
$text = preg_replace('/u([0-9a-f]{4})/i', '&#x$1;', $text);
echo $text;  //displays Wladisław
?>

答案 2 :(得分:0)

以下代码允许您解码字符,并在必要时重新编码

代码

if (!function_exists('codepoint_encode')) {

    function codepoint_encode($str) {
        return substr(json_encode($str), 1, -1);
    }

}

if (!function_exists('codepoint_decode')) {

    function codepoint_decode($str) {
        return json_decode(sprintf('"%s"', $str));
    }

}

如何使用

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

var_dump(codepoint_encode('ඔන්ලි'));
var_dump(codepoint_encode('සින්ග්ලිෂ්'));

var_dump(codepoint_decode('\u0d94\u0db1\u0dca\u0dbd\u0dd2'));
var_dump(codepoint_decode('\u0dc3\u0dd2\u0db1\u0dca\u0d9c\u0dca\u0dbd\u0dd2\u0dc2\u0dca'));

输出

string(30) "\u0d94\u0db1\u0dca\u0dbd\u0dd2"
string(60) "\u0dc3\u0dd2\u0db1\u0dca\u0d9c\u0dca\u0dbd\u0dd2\u0dc2\u0dca"
string(15) "ඔන්ලි"
string(30) "සින්ග්ලිෂ්"

如果您想要更复杂的功能,请参阅How to get the character from unicode code point in PHP?