note 116271 added to function.utf8-decode
| From: | sashott at gmail dot com | Date: | Thu, 04 Dec 2014 11:57:38 +0000 |
| Subject: | note 116271 added to function.utf8-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201063@lists.php.net to get a copy of this message | ||
Use of utf8_decode was not enough for me by get page content from another site. Problem appear by
different alphabet from standard latin. As example some chars (corresponding to HTML codes
„ , and others) are converted to "?" or "xA0" (hex value).
You need to make some conversion before execute utf8_decode. And you can not replace simple, that
they can be part of 2 bytes code for a char (UTF-8 use 2 bytes). Next is for cyrillic alphabet, but
for other must be very close.
function convertMethod($text){
//Problem is that utf8_decode convert HTML chars for „ and other to ? or to
\xA0. And you can not replace, that they are in some char bytes and you broke cyrillic (or other
alphabet) chars.
$problem_enc=array(
'euro',
'sbquo',
'bdquo',
'hellip',
'dagger',
'Dagger',
'permil',
'lsaquo',
'lsquo',
'rsquo',
'ldquo',
'rdquo',
'bull',
'ndash',
'mdash',
'trade',
'rsquo',
'brvbar',
'copy',
'laquo',
'reg',
'plusmn',
'micro',
'para',
'middot',
'raquo',
'nbsp'
);
$text=mb_convert_encoding($text,'HTML-ENTITIES','UTF-8');
$text=preg_replace('#(?<!\Ð)\&('.implode('|',$problem_enc).');#s','--amp{$1}',$text);
$text=mb_convert_encoding($text,'UTF-8','HTML-ENTITIES');
$text=utf8_decode($text);
$text=mb_convert_encoding($text,'HTML-ENTITIES','UTF-8');
$text=preg_replace('#\-\-amp\{([^\}]+)\}#su','&$1;',$text);
$text=mb_convert_encoding($text,'UTF-8','HTML-ENTITIES');
return $text;
}
If this don't work, try to set "die($text);" on some places to look, what is happen
to this row. Is better to test with long text. It is very possible to broke other alphabet
character. In this case, it is very possible, that for you alphabet set "Ð" is not
the right one. You need to set "die($text);" after this preg_replace and look HTML code
for character before set "--amp".
----
Server IP: 72.52.91.14
Probable Submitter: 94.26.14.146
----
Manual Page -- http://php.net/manual/en/function.utf8-decode.php
Edit -- https://master.php.net/note/edit/116271
Del: integrated -- https://master.php.net/note/delete/116271/integrated
Del: useless -- https://master.php.net/note/delete/116271/useless
Del: bad code -- https://master.php.net/note/delete/116271/bad+code
Del: spam -- https://master.php.net/note/delete/116271/spam
Del: non-english -- https://master.php.net/note/delete/116271/non-english
Del: in docs -- https://master.php.net/note/delete/116271/in+docs
Del: other reasons-- https://master.php.net/note/delete/116271
Reject -- https://master.php.net/note/reject/116271
Search -- https://master.php.net/manage/user-notes.php