How can I change the encoding from utf8 to big endian

samilac226 :

How can I change the encoding from utf8 to big-endian only for Arabic characters to send SMS messages through SMS service? This code doesn't work.

mb_convert_encoding($str, "UCS-2BE", "auto");

Result should look like this

Marat Badykov :

You must do the following:

  1. check the encoding and the presence of Arabic characters. You can do it like that:

     //$text - string that you need to convert
    
     if (mb_detect_encoding ($text) == "UTF-8" && mb_ereg('[\x{0600}-\x{06FF}]', $text)) {
                ...
     }
    
  2. Convert string to USC-2BE encoding.You can use iconv function

    iconv("UTF-8", "UCS-2BE", $text)
    
  3. Then unpack to needed format (H*hex - in your case) and convert to uppercase:

    if (mb_detect_encoding ($text) == "UTF-8" && self::isArabic($text)) {
                $arr = unpack("H*hex", @iconv("UTF-8", "UCS-2BE", $text));
                $text = strtoupper($arr["hex"]);
    }
    

That's it. It should work.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=342193&siteId=1