我想将unicode转换为字符。
为此,我尝试了https://stackoverflow.com/a/7278961/4073217帖子,但它不适用于我。
例如:
$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');
输出应该是Αθανασίου
,但上面的方法会返回Αb8b1bdb1c3afbfc5
。
我做错了吗?如何在php中从Unicode获取正确的字符?
答案 0 :(得分:4)
正则表达式:
$string = preg_replace('/%u([0-9A-F]+)/', '&#x$1;', $string)
有A-F,这意味着它只会比较0-9之后的大写A-F字符。由于你有所有小字符匹配失败。尝试:
$string = preg_replace('/%u([0-9a-f]+)/', '&#x$1;', $string);
代替。
同时检查浏览器输出是否为utf-8。如果没有,你可以使用标题:
header('Content-type: text/html; charset=utf-8');
在回显输出之前
答案 1 :(得分:1)
<?php
header('Content-type: text/html; charset=utf-8');
$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');
$arr = [
'to_email' => 'sender@email.com',
'from_email' => 'receiver@email.com',
'subject' => 'utf',
'message' => $string
];
mail_send($arr);
function mail_send($arr)
{
if (!isset($arr['to_email'], $arr['from_email'], $arr['subject'], $arr['message'])) {
throw new HelperException('mail(); not all parameters provided.');
}
$to = empty($arr['to_name']) ? $arr['to_email'] : '"' . mb_encode_mimeheader($arr['to_name']) . '" <' . $arr['to_email'] . '>';
$from = empty($arr['from_name']) ? $arr['from_email'] : '"' . mb_encode_mimeheader($arr['from_name']) . '" <' . $arr['from_email'] . '>';
$headers = array
(
'MIME-Version: 1.0',
'Content-Type: text/html; charset="UTF-8";',
'Content-Transfer-Encoding: 7bit',
'Date: ' . date('r', $_SERVER['REQUEST_TIME']),
'Message-ID: <' . $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '@' . $_SERVER['SERVER_NAME'] . '>',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
'X-Mailer: PHP v' . phpversion(),
'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'],
);
mail($to, '=?UTF-8?B?' . base64_encode($arr['subject']) . '?=', $arr['message'], implode("\n", $headers));
}
这将在浏览器中打印Αθανασίου
并在电子邮件中发送电子邮件Αθανασίου