note 90330 added to function.iconv
| From: | manuel at kiessling dot net | Date: | Thu, 16 Apr 2009 16:33:01 +0000 |
| Subject: | note 90330 added to function.iconv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-153396@lists.php.net to get a copy of this message | ||
Like many other people, I have encountered massive problems when using iconv() to convert between
encodings (from UTF-8 to ISO-8859-15 in my case), especially on large strings.
The main problem here is that when your string contains illegal UTF-8 characters, there is no really
straight forward way to handle those. iconv() simply (and silently!) terminates the string when
encountering the problematic characters (also if using //IGNORE), returning a clipped string. The
<?php
$newstring = html_entity_decode(htmlentities($oldstring, ENT_QUOTES, 'UTF-8'), ENT_QUOTES
, 'ISO-8859-15');
?>
workaround suggested here and elsewhere will also break when encountering illegal characters, at
least dropping a useful note ("htmlentities(): Invalid multibyte sequence in argument
in...")
I have found a lot of hints, suggestions and alternative methods (it's scary and in my opinion
no good sign how many ways PHP natively provides to convert the encoding of strings), but none of
them really worked, except for this one:
<?php
$newstring = mb_convert_encoding($oldstring, 'ISO-8859-15', 'UTF-8');
?>
----
Server IP: 212.124.37.9
Probable Submitter: 195.167.217.249
----
Manual Page -- http://www.php.net/manual/en/function.iconv.php
Edit -- https://master.php.net/note/edit/90330
Del: integrated -- https://master.php.net/note/delete/90330/integrated
Del: useless -- https://master.php.net/note/delete/90330/useless
Del: bad code -- https://master.php.net/note/delete/90330/bad+code
Del: spam -- https://master.php.net/note/delete/90330/spam
Del: non-english -- https://master.php.net/note/delete/90330/non-english
Del: in docs -- https://master.php.net/note/delete/90330/in+docs
Del: other reasons-- https://master.php.net/note/delete/90330
Reject -- https://master.php.net/note/reject/90330
Search -- https://master.php.net/manage/user-notes.php