简体   繁体   中英

Converting SMS encoding to UTF-8 in PHP

I wrote an SMPP Server Transceiver in PHP. I get this SMS string from my SMPP. It's a UTF8 message which is actually at 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.

I am trying to achieve this using 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. all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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