Encoded data and spaces

Hai folks,

My sms provider require all unicode sms sent to be formated to USC-2 Encoding.

Text to be sent : درويش
UCS-2 Encoded : 0 b a 4 0 b b f 0 b b 0 0 b c

function used for the conversion :

<?php
function convert($str){
	$str = mb_convert_encoding(bin2hex(mb_convert_encoding($str, 'UCS-2', 'auto')), 'UCS-2', 'auto');
	return $str;
}
?>

Now, you can see converted arabic text in ucs-2 format and you see every character is proceeding a space.

Strange, i could not remove those spaces using the str_replace. Because the sms api require those spaces removed.

<?php
function convert($str){
	$str = mb_convert_encoding(bin2hex(mb_convert_encoding($str, 'UCS-2', 'auto')), 'UCS-2', 'auto');
	return str_replace(' ','',$str);
}
?>

What could be the problem :rolleyes:

  • when i echo the text,the spaces are not showing in browser. but i want to fopen in my case.

Then the spaces are not truly spaces, they are likely considered “low” values (meaning before the space on the ASCII table/your encoding table).

You may want to try this (although it may still not work, because the value may not match)

$str = preg_replace('/[\\s]/', '', $str); //replaces whitespace, form feeds, tabs, carriage returns and new lines

Another alternative, is if you are expecting only letters and numbers in your conversion, you can remove anything that is not a letter or number

$str = preg_replace('/[^a-z0-9]+/i', '', $str);

Another alternative, is if you are expecting only letters and numbers in your conversion, you can remove anything that is not a letter or number

$str = preg_replace('/[^a-z0-9]+/i', '', $str);

Workes charm!!!
Thank you cpradio :smiley: