Sushil Kandola
Sushil Kandola

Reputation: 880

iconv(): Detected an illegal character in input string

I am using Iconv function to convert string to requested character encoding. Look On Below Code

$sms_text =  'A:'f3*'F'; // Output received from SMPP
$result = iconv('UTF-16BE' ,'UTF-8//IGNORE' , $sms_text);
echo 'Ignore: ' .$result;

echo $sms_text = iconv('UTF-16BE' ,'UTF-8' , $sms_text);

$result1 = iconv('UTF-16BE' ,'UTF-8//TRANSLIT' , $sms_text); //line no (53)
echo 'Transilt: '.$result1;

And I received the below Output enter image description here
If I have a string of dari and pashto Language, then its showing only first word and dont return remaining string after blank space. Even //IGNORE gives the same output.

Should I replace these blank spaces with the help of with some other character so that I can get complete string?

Note: I am passing string received from SMPP(receiver).

SMS Sent to SMPP : افغانستان کابل
Outpur Received from SMPP : 'A:'f3*'F
String back converted by iconv : افغانستان 

English string is working well.

Thanks in advance.

Upvotes: 1

Views: 24842

Answers (1)

Sergii Ivashchenko
Sergii Ivashchenko

Reputation: 543

You are converting string to UTF-8 charset on this line:

echo $sms_text = iconv('UTF-16BE' ,'UTF-8' , $sms_text);

The error appears becouse you are trying to convert it second time. To resolve this issue you should not update $sms_text variable on mentioned line.

Here is a code and output that works for me:

$sms_text ="فغانستان کابل";

echo "UTF-8 : $sms_text \n";

$sms_text = iconv('UTF-8', 'UTF-16BE', $sms_text);

echo "UTF-16BE : $sms_text \n";

echo 'Ignore: ' . iconv('UTF-16BE' ,'UTF-8//IGNORE' , $sms_text) . "\n";
echo 'Simple: ' . iconv('UTF-16BE' ,'UTF-8' , $sms_text) . "\n";
echo 'Transilt: '. iconv('UTF-16BE' ,'UTF-8//TRANSLIT' , $sms_text) . "\n";

Output:

UTF-8 : فغانستان کابل 
UTF-16BE : A:'F3*'F
Ignore: فغانستان کابل
Simple: فغانستان کابل
Transilt: فغانستان کابل

As for blank spaces, could you please share the test string?

Upvotes: 6

Related Questions