Dynamic unicode message content

43 views Asked by At

My message body is dynamic and sometimes it contain apostrophe (`). For eg: Swifty’s Car

Message is not displaying correctly on the receipent phone "Swiftya€?s Car"

How can I fix this? I can not use the "Unicode Converter" tool since my message body is dynamic.

https://api.clickatell.com/http/sendmsg?user=XXX&password=XXX&api_id=XXX&to=XXX&text=Swifty’s Car

Ouput: Swiftya€?s Car

Then I converted the apostrophe to unicode using "Unicode Converter" tool

https://api.clickatell.com/http/sendmsg?user=XXX&password=XXX&api_id=XXX&to=XXX&text=Swifty2019s Car&unicode=1

Ouput: ERR: 116, Invalid Unicode data

2

There are 2 answers

0
Vvikas Gupta On BEST ANSWER

I solved this by converting it to unicode using following script

$smsBody = mb_convert_encoding($smsBody, "UTF-16");
$smsBody = bin2hex($smsBody);
0
whatever_sa On

Alternative solution: In your example you could convert ’ to ' which is in the GSM character set. If you have an interface where users type text, you could limit what characters they are allowed to use.

How to convert to Unicode: Since not all characters fall within GSM charset, it may be easier to just convert all the messages.

Here is an example of how to convert your message text to a unicode string using iconv:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<form method='POST' action=''>
<textarea name='data'></textarea>
<input type='submit'>

<?php

if (isset($_REQUEST['data'])) {
    $data2 = iconv("UTF-8", "UCS-2BE", $_REQUEST['data']);
    $output = bin2hex($data2);
    echo $output;
}

Then you send your message using that string:

https://api.clickatell.com/http/sendmsg?user=XXX&password=XXX&api_id=XXX&to=XXX&text=005400680069007300200069007300200075006E00690063006F00640065002E20AC002E005400680069007300200069007300200075006E00690063006F00640065002C002E005400680069007300200069007300200075006E00690063006F00640065002E20AC002E005400680069007300200069007300200075006E00690063006F00640065002C002E&unicode=1

This example works...One of the issues you may encounter is that Unicode messages cannot be as long as text messages.