Jump to content

php, iconv() function error message


deotpit

Recommended Posts

Hi everyone,
My text file contains the following string: "ãéðä".
My PHP batch to convert that string goes like this:
<?php
$text = iconv("UTF-8","ISO-8859-1", "ãéðä");
echo $text;
?>
When I run the code I get the converted text I want .
In a case i have more then one such string, I use the following code:
<?php
$myFile = 'test.txt';
$myHandle = fopen($myFile,'r');
$myText = fread($myHandle, filesize($myFile));
$ridComma = explode(',',$myText);
foreach($ridComma as $item)
{
$text = iconv("UTF-8","ISO-8859-1", $item);
}
fclose($myHandle);
?>
This time I get the following error:
Notice: iconv(): Detected an illegal character in input string in C:\xampp\htdocs\test\test.php on line 8

 

With the same conversion function I get the conversion in one batch and an error in the other !

Could anyone explain me why?
Thanks !
Link to comment
https://forums.phpfreaks.com/topic/292468-php-iconv-function-error-message/
Share on other sites

Encoding is messy and sometimes difficult.

 

You first need to detect the encoding and iconv doesn't do that.

mb_detect_encoding()

 

It's possible to use the header of the file info or a pages charset info, but is not always accurate or mixed encodings.

 

This person made an always utf-8 class that could help.

It could also fix garbled utf-8

https://github.com/neitanod/forceutf8

 

Even if you want to encode it to something else...at least it should be utf-8 to start with.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.