Jump to content

php, iconv() function error message


deotpit
Go to solution Solved by requinix,

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
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.

Edited by QuickOldCar
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.