Converting encodings for Clickatell’s UTF support.

My picI know this is a port mania today but i finally solved a problem that has been bugging me for a few days now. I wanted to send an sms via Clicaktell’s gateway that i wanted it to be utf. This gateway supports this kind of messages but with a trick.

When you set the UTF support on your message, your message needs to be UTF-16 url encoded. This is tricky. For instance the folloring will not work.

  1. urlencode(mb_convert_encoding($text, ‘UTF-16′, ‘UTF-8′))

With this you will get an error message saying that your utf data are not right. Please notice that i used mb_convert_encoding from the mbstring module in php (more about the mbstring module in php’s manual here). So here is a function i found on php.net’s manual and i modified a little bit to use.

  1. function utf16urlencode($str)
  2. {
  3.     $str = mb_convert_encoding($str, ‘UTF-16′, ‘UTF-8′);
  4.     $out = ;
  5.     for ($i = 0; $i < mb_strlen($str, ‘UTF-16′); $i++)
  6.     {
  7.         $out .= bin2hex(mb_substr($str, $i, 1, ‘UTF-16′));
  8.     }
  9.     return $out;
  10. }

Using the function above you can convert your string to add it in the url of the message sending. Hope this helps!



6 Responses to “ “Converting encodings for Clickatell’s UTF support.”

  1. Trax says:

    THANKS MAN :)

  2. Sjarel says:

    Great code! Not perfect in all cases, but still it’s by far the best I’ve found.

  3. stratosg says:

    @Sjarel: thanks for commenting… it could be better indeed but still it does what it is intended to do :)

  4. Sjarel says:

    To be more precise, I hope it can be helpful : When I try to send ‘Družstevník’ with this function, I receive ‘Drustevník’ on my mobile. So it does the job indeed, but one character was lost.

  5. stratosg says:

    hm… well it seems that that ž can’t be converted to UTF-16 or something. it must be that… sorry i can’t help you more but i am not aware of the language and how characters stored and converted…

  6. Sjarel says:

    No problem, just wanted to let you know …

Leave a Reply