logo
Tamer Hatoum

UTF-8 to UCS-2

Posted by Tamer Hatoum in PHP

08-Dec-09 12:02 PM

Hello ;

I am searching for a convertor function from UTF-8 to UCS-2 can any body help?

regards...

Reply Reply Using Power Editor

re

Web star replied to Tamer Hatoum

08-Dec-09 12:32 PM

look here

<?php
//script from http://zizi.kxup.com/
//javascript unesape
function unescape($str) {
 
$str = rawurldecode($str
);
 
preg_match_all("/(?:%u.{4})|&#x.{4};|&#\d+;|.+/U",$str,$r
);
 
$ar = $r[0
];
print_r($ar
);
  foreach(
$ar as $k=>$v
) {
    if(
substr($v,0,2) == "%u"
)
     
$ar[$k] = iconv("UCS-2","UTF-8",pack("H4",substr($v,-4
)));
    elseif(
substr($v,0,3) == "&#x"
)
     
$ar[$k] = iconv("UCS-2","UTF-8",pack("H4",substr($v,3,-1
)));
    elseif(
substr($v,0,2) == "&#"
) {
echo
substr($v,2,-1)."<br>"
;
     
$ar[$k] = iconv("UCS-2","UTF-8",pack("n",substr($v,2,-1
)));
    }
  }
  return
join("",$ar
);
}
?>

http://php.net/manual/en/function.iconv.php



Reply Reply Using Power Editor
[)ia6l0 iii

You can convert character encoding using the following two options:

[)ia6l0 iii replied to Tamer Hatoum

EggHeadCafe's human moderators scored this post for our messageboard contest.

08-Dec-09 01:23 PM

  • The iConv character set can let you convert from one character encoding to another.  Here are the references that you need on the iConv character conversions.
  • Or use the mb_Convert_Encoding function by specifying the from and the to target encodings like:        $cstr = mb_convert_encoding($str,"UCS2","UTF-8");                                                                    The above mb_Convert_Encoding  functions converts string from UTF-8 to UCS2


Biography
I don't want to have my biography in every page that you read. But if you need , ask!

Site Rank:  Not applicable - Current Winnings:  $0.00


Reply Reply Using Power Editor
Tamer Hatoum

SMS WEB convert

Tamer Hatoum replied to Web star

09-Dec-09 03:26 AM

Hello;

I need this convert to use in my web sms service , couse the company which I am deailing with I have to vivst there web to make the convert to the string the copy it to my web page then send it...

so What I want to make my own convertor.

so I tried this :

<?
 
 
function utf8toucs2hex($utf8)
{
 $utf8_hex = bin2hex( $utf8 );
 return utf8toucs2($utf8_hex);
}

function utf8hextoucs2hex($str)
{
       $ucs2 = "";

       for ($i=0;$i<strlen($str);$i+=2)
       {
                $char1hex = $str[$i].$str[$i+1];
              
  $char1dec = hexdec($char1hex);
                if ( $char1dec < 128)
  {
                        $results = $char1hex;
  }
                else if ( $char1dec < 224 )
                {
                 $char2hex = $str[$i+2].$str[$i+3];
                        $results = dechex( ((hexdec($char1hex)-192)*64) + (hexdec($char2hex)-128) );
                        $i+=2;
                }
                else if ( $char1dec < 240 )
                {
                 $char2hex = $str[$i+2].$str[$i+3];
                 $char3hex = $str[$i+4].$str[$i+5];
                        $results = dechex( ((hexdec($char1hex)-224)*4096) + ((hexdec($char2hex)-128)*64) + (hexdec($char3hex)-128) );
                        $i+=4;
                }
  else
  {
   //Not supported: UCS-2 only
                        $i+=6;
  }

  while ( strlen($results) < 4 )
  {
   $results = '0' . $results;
  }

                $ucs2 .= $results;
        }

        return $ucs2;
}

 $error="";
 if (isset ($_POST['submit']) && !empty($_POST['Mobile'])&& !empty($_POST['msg'])) {
$user = "username";
$password = "password";
$api_id ="apID";
$baseurl ="http://api.clickatell.com";
$text =$_POST['msg'];
$message_unicodehex = utf8toucs2hex($text);
$to = $_POST['Mobile'];
// auth call
$url = "$baseurl/http/auth?user=$user&password=$password&api_id=$api_id&unicode=1";
// do auth call
$ret = file($url);
$from="QN";
// split our response. return string is on first line of the data returned
$sess = split(":",$ret[0]);
if ($sess[0] == "OK") {
$sess_id = trim($sess[1]); // remove any whitespace

$url = "$baseurl/http/sendmsg?session_id=$sess_id&to=$to&text=$message_unicodehex&from=$from";
// do sendmsg call
$ret = file($url);
$send = split(":",$ret[0]);
if ($send[0] == "ID")
$error="success
message ID: ". $send[1];
else
$error='send message failed <br>check the mobile number format to be without + and without 00 ( 9746026267)';
} else {
$error="Authentication failure: ". $ret[0];
exit();
}
}
else $error="Plz Fill All the fields";
?>

So when I try the string in english it return to me the same string when I use there convertor. it works the same as there convertor , but if I put an arabic string my convertor gives defferent one then there convertor result...

what can I do?

regards..



Reply Reply Using Power Editor
Tamer Hatoum

SOLVED

Tamer Hatoum replied to Tamer Hatoum

09-Dec-09 12:51 PM

Hello All;

I have solved the problem and make that function of convertor:

function sms__unicode($message) {
  if (function_exists('iconv')) {
    $latin = @iconv('UTF-8', 'ISO-8859-1', $message);
    if (strcmp($latin, $message)) {
      $arr = unpack('H*hex', @iconv('UTF-8', 'UCS-2BE', $message));
      return strtoupper($arr['hex']) .'&unicode=1';
    }
  }
  return FALSE;
}

It works great with arabic unicoding .....

End of Post...



Reply Reply Using Power Editor


digg facebook google buzz reddit del.icio.us stumble upon twitter

Didn't Find The Answer You Were Looking For?

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum









Pete's Resume  |  Robbe's Resume  |  Neado  |  Free Icons  |  Privacy  |   (c) 2010