1

我在 php 中保存了一条记录“فحص الرسالة العربية”,该记录始终保存为:

فحص الرسالة العربية

我想在检索它时将其转换为 UTF-16BE 字符,因此我使用了一个返回的函数:

002600230031003600300031003b002600230031003500380031003b002600230031003500380039003b0020002600230031003500370035003b002600230031003600300034003b002600230031003500380035003b002600230031003500380037003b002600230031003500370035003b002600230031003600300034003b002600230031003500370037003b0020002600230031003500370035003b002600230031003600300034003b002600230031003500390033003b002600230031003500380035003b002600230031003500370036003b002600230031003600310030003b002600230031003500370037003b

这是 m 用于转换从数据库检索到的字符串的函数

function convertCharsn($string) {
    $in = '';
    $out = iconv('UTF-8', 'UTF-16BE', $string);
    for($i=0; $i<strlen($out); $i++) {
      $in .= sprintf("%02X", ord($out[$i]));
    }
    return $in;
}

但是当我在下面的 url 中输入相同的字符时,它显示的字符与我的字符串不同。 http://www.routesms.com/downloads/onlineunicode.asp

返回:

0641062D063500200627064406310633062706440629002006270644063906310628064A0629

我希望我的字符串在上面的 url 中被转换时被转换。我的数据库排序规则是 utf-8_general_ci

4

1 回答 1

2

基本上,您需要首先从 HTML 实体中解码这些字符。只需使用html_entity_decode()

$rawChars = html_entity_decode($string, ENT_QUOTES | ENT_HTML401, 'UTF-8');

convertCharsn($rawChars);

否则,您只是对实体进行编码。您可以看到UTF16 中的原样,&并且是. 因此您可以看到您发布的上述转码中的重复序列。所以先解码它,你应该被设置...0026#002300260023

于 2012-05-01T13:08:58.507 回答