简体   繁体   English

在PHP中将SMS编码转换为UTF-8

[英]Converting SMS encoding to UTF-8 in PHP

I wrote an SMPP Server Transceiver in PHP. 我用PHP编写了SMPP服务器收发器。 I get this SMS string from my SMPP. 我从我的SMPP获得此SMS字符串。 It's a UTF8 message which is actually at 7Bit. 这是一条UTF8消息,实际上是7Bit。 Here is a sample message: 这是一个示例消息:

  5d30205d30205d3

I know how to convert it. 我知道如何转换。 It should be: 它应该是:

  \x5d3\x020\x5d3\x020\x5d3

I don't want to write it myself. 我不想自己写。 I guess there is already a function that does that for me. 我想已经有一个函数可以帮我实现。 Some hidden iconv or using pack() / unpack() to convert this string to the correct format. 一些隐藏的iconv或使用pack()/ unpack()将此字符串转换为正确的格式。

I am trying to achieve this using PHP. 我正在尝试使用PHP实现此目的。 Any ideas? 有任何想法吗?

Thanks. 谢谢。

This should do it : 这应该做到这一点:

$message = "5d30205d30205d3";
echo "\x".implode("\x", str_split($message, 3));
// \x5d3\x020\x5d3\x020\x5d3

Here is what i used eventually: 这是我最终使用的:

public static function sms__from_unicode($message)
{
    $org_msg = str_split(strtolower($message), 3);
    for($i = 0;$i < count($org_msg); $i++)
        $org_msg[$i] = "\u0{$org_msg[$i]}";

    $str = implode(null, $org_msg);
    $str = preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $str);    
    return $str;
}   

function replace_unicode_escape_sequence($match) {
     return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}

10x. 10倍 all. 所有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM