deotpit Posted November 14, 2014 Share Posted November 14, 2014 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/292468-php-iconv-function-error-message/ Share on other sites More sharing options...
Solution requinix Posted November 14, 2014 Solution Share Posted November 14, 2014 Apparently the file isn't in the ISO 8859-1 encoding. Quote Link to comment https://forums.phpfreaks.com/topic/292468-php-iconv-function-error-message/#findComment-1496611 Share on other sites More sharing options...
QuickOldCar Posted November 14, 2014 Share Posted November 14, 2014 (edited) 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 November 14, 2014 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/292468-php-iconv-function-error-message/#findComment-1496613 Share on other sites More sharing options...
deotpit Posted November 15, 2014 Author Share Posted November 15, 2014 Thank you both ! You solved my problem but for some reason I don't know, I couldn't "like" it Quote Link to comment https://forums.phpfreaks.com/topic/292468-php-iconv-function-error-message/#findComment-1496677 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.