PHP 7.2.0 Release Candidate 4 Released

utf8_decode

(PHP 4, PHP 5, PHP 7)

utf8_decode Konvertiert eine als UTF-8 kodierte ISO-8859-1-Zeichenkette in eine einfache ISO-8859-1-Zeichenkette

Beschreibung

string utf8_decode ( string $data )

Diese Funktion konvertiert die Zeichenkette data von der UTF-8 Kodierung nach ISO-8859-1. Bytes in der Zeichenkette, die kein gültiges UTF-8 sind, und UTF-8 Zeichen, die nicht in ISO-8859-1 existieren (also Zeichen größer als U+00FF), werden durch ? ersetzt.

Hinweis:

Viele Webseiten, die ausgewiesen sind die ISO-8859-1 Zeichenkodierung zu verwenden, nutzen in Wirklichkeit die ähnliche Windows-1252 Kodierung, und Webbrowser interpretieren ISO-8859-1 Webseiten als Windows-1252. Windows-1252 ermöglicht zusätzliche druckbare Zeichen, so wie das Euro-Zeichen () und geschweifte Anführungszeichen () anstelle von bestimmten ISO-8859-1 Kontrollzeichen. Diese Funktion konvertiert solche Windows-1252 Zeichen nicht korrekt. Verwenden Sie eine andere Funktion, wenn Windows-1252 Konvertierung erforderlich ist.

Parameter-Liste

data

Eine UTF-8-kodierte Zeichenkette.

Rückgabewerte

Gibt die ISO-8859-1-Übersetzung von data zurück.

Siehe auch

  • utf8_encode() - Konvertiert eine ISO-8859-1-Zeichenkette in UTF-8 - Führt die umgekehrte Konvertierung durch
  • mb_convert_encoding() - Convert character encoding - Konvertiert zwischen verschiedenen Zeichenkodierungen, einschließlich UTF-8, ISO-8859-1 und Windows-1252
  • iconv() - Konvertiert Zeichenketten in einen anderen Zeichensatz - Konvertiert zwischen verschiedenen Zeichenkodierungen
  • recode_string() - Umkodierung eines Strings entsprechend einer Recode-Anweisung - Konvertiert zwischen verschiedenen Zeichenkodierungen

add a note add a note

User Contributed Notes 35 notes

up
19
deceze at gmail dot com
6 years ago
Please note that utf8_decode simply converts a string encoded in UTF-8 to ISO-8859-1. A more appropriate name for it would be utf8_to_iso88591. If your text is already encoded in ISO-8859-1, you do not need this function. If you don't want to use ISO-8859-1, you do not need this function.

Note that UTF-8 can represent many more characters than ISO-8859-1. Trying to convert a UTF-8 string that contains characters that can't be represented in ISO-8859-1 to ISO-8859-1 will garble your text and/or cause characters to go missing. Trying to convert text that is not encoded in UTF-8 using this function will most likely garble the text.

If you need to convert any text from any encoding to any other encoding, look at iconv() instead.
up
13
info at vanylla dot it
8 years ago
IMPORTANT: when converting UTF8 data that contains the EURO sign DON'T USE utf_decode function.

utf_decode converts the data into ISO-8859-1 charset. But ISO-8859-1 charset does not contain the EURO sign, therefor the EURO sign will be converted into a question mark character '?'

In order to convert properly UTF8 data with EURO sign you must use:

iconv("UTF-8", "CP1252", $data)
up
5
gabriel arobase gabsoftware dot com
6 years ago
If you want to retrieve some UTF-8 data from your database, you don't need utf8_decode().

Simply do the following query before any SELECT :

$result = mysql_query("SET NAMES utf8");
up
5
okx dot oliver dot koenig at gmail dot com
2 years ago
// This finally helped me to do the job, thanks to Blackbit, had to modify deprecated ereg:
// original comment: "Squirrelmail contains a nice function in the sources to convert unicode to entities:"

function charset_decode_utf_8 ($string) {
    /* Only do the slow convert if there are 8-bit characters */
    /* avoid using 0xA0 (\240) in ereg ranges. RH73 does not like that */
    if (!preg_match("/[\200-\237]/", $string)
     && !preg_match("/[\241-\377]/", $string)
    ) {
        return $string;
    }

    // decode three byte unicode characters
    $string = preg_replace("/([\340-\357])([\200-\277])([\200-\277])/e",
        "'&#'.((ord('\\1')-224)*4096 + (ord('\\2')-128)*64 + (ord('\\3')-128)).';'",
        $string
    );

    // decode two byte unicode characters
    $string = preg_replace("/([\300-\337])([\200-\277])/e",
        "'&#'.((ord('\\1')-192)*64+(ord('\\2')-128)).';'",
        $string
    );

    return $string;
}
up
3
thierry.bo # netcourrier point com
12 years ago
In response to fhoech (22-Sep-2005 11:55), I just tried a simultaneous test with the file UTF-8-test.txt using your regexp, 'j dot dittmer' (20-Sep-2005 06:30) regexp (message #56962), `php-note-2005` (17-Feb-2005 08:57) regexp in his message on `mb-detect-encoding` page (http://us3.php.net/manual/en/function.mb-detect-encoding.php#50087) who is using a regexp from the W3C (http://w3.org/International/questions/qa-forms-utf-8.html), and PHP mb_detect_encoding function.

Here are a summarize of the results :

201 lines are valid UTF8 strings using phpnote regexp
203 lines are valid UTF8 strings using j.dittmer regexp
200 lines are valid UTF8 strings using fhoech regexp
239 lines are valid  UTF8 strings using using mb_detect_encoding

Here are the lines with differences (left to right, phpnote, j.dittmer and fhoech) :

Line #70 : NOT UTF8|IS UTF8!|IS UTF8! :2.1.1 1 byte (U-00000000): ""
Line #79 : NOT UTF8|IS UTF8!|IS UTF8! :2.2.1 1 byte (U-0000007F): ""
Line #81 : IS UTF8!|IS UTF8!|NOT UTF8 :2.2.3 3 bytes (U-0000FFFF): "" |
Line #267 : IS UTF8!|IS UTF8!|NOT UTF8 :5.3.1 U+FFFE = ef bf be = "" |
Line #268 : IS UTF8!|IS UTF8!|NOT UTF8 :5.3.2 U+FFFF = ef bf bf = "" |

Interesting is that you said that your regexp corrected j.dittmer regexp that failed on 5.3 section, but it my test I have the opposite result ?!

I ran this test on windows XP with PHP 4.3.11dev. Maybe these differences come from operating system, or PHP version.

For mb_detect_encoding I used the command :

mb_detect_encoding($line, 'UTF-8, ISO-8859-1, ASCII');
up
1
Aidan Kehoe <php-manual at parhasard dot net>
13 years ago
The fastest way I've found to check if something is valid UTF-8 is
<?php
if (iconv('UTF-8', 'UTF-8', $input) != $input) {
       
/* It's not UTF-8--for me, it's probably CP1252, the Windows
           version of Latin 1, with directed quotation marks and
           the Euro sign.  */
}
?>.
The iconv() C library fails if it's told a string is UTF-8 and it isn't; the PHP one doesn't, it just returns the conversion up to the point of failure, so you have to compare the result to the input to find out if the conversion succeeded.
up
1
sashott at gmail dot com
2 years ago
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 &bdquo; , &nbsp; 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 &bdquo; and other to ? or &nbsp; 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('#(?<!\&ETH;)\&('.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 "&ETH;" is not the right one. You need to set "die($text);" after this preg_replace and look HTML code for character before set "--amp".
up
0
Aleksandr
3 months ago
In addition to note by yannikh at gmeil dot com, another way to decode strings with non-latin chars from unix console like

C=RU, L=\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0,

<?php preg_replace_callback('/\\\\x([0-9A-F]{2})/', function($a){ return pack('H*', $a[1]); }, $str); ?>

The code above will output:
C=RU, L=Москва,
up
0
kode68
1 year ago
Update Answer from okx dot oliver dot koenig at gmail dot com for PHP 5.6 since e/ modifier is depreciated

// This finally helped me to do the job, thanks to Blackbit, had to modify deprecated ereg:
// original comment: "Squirrelmail contains a nice function in the sources to convert unicode to entities:"

function charset_decode_utf_8($string)
    {
        /* Only do the slow convert if there are 8-bit characters */
        if ( !preg_match("/[\200-\237]/", $string) && !preg_match("/[\241-\377]/", $string) )
               return $string;

        // decode three byte unicode characters
          $string = preg_replace_callback("/([\340-\357])([\200-\277])([\200-\277])/",
                    create_function ('$matches', 'return \'&#\'.((ord($matches[1])-224)*4096+(ord($matches[2])-128)*64+(ord($matches[3])-128)).\';\';'),
                    $string);

        // decode two byte unicode characters
          $string = preg_replace_callback("/([\300-\337])([\200-\277])/",
                    create_function ('$matches', 'return \'&#\'.((ord($matches[1])-192)*64+(ord($matches[2])-128)).\';\';'),
                    $string);

        return $string;
    }

Enjoy
up
0
christoffer
4 years ago
The preferred way to use this on an array would be with the built in PHP function "array_map()", as for example:
$array = array_map("utf8_decode", $array);
up
0
gto at interia dot pl
13 years ago
Correction to function converting utf82iso88592 and iso88592tutf8.
Janusz forgot about "&#324;", and "&#380;" exchanged from "&#378;" here and there.

GTo

function utf82iso88592($tekscik) {
     $tekscik = str_replace("\xC4\x85", "&#261;", $tekscik);
     $tekscik = str_replace("\xC4\x84", '&#260;', $tekscik);
     $tekscik = str_replace("\xC4\x87", '&#263;', $tekscik);
     $tekscik = str_replace("\xC4\x86", '&#262;', $tekscik);
     $tekscik = str_replace("\xC4\x99", '&#281;', $tekscik);
     $tekscik = str_replace("\xC4\x98", '&#280;', $tekscik);
     $tekscik = str_replace("\xC5\x82", '&#322;', $tekscik);
     $tekscik = str_replace("\xC5\x81", '&#321;', $tekscik);
     $tekscik = str_replace("\xC5\x84", '&#324;', $tekscik);    
     $tekscik = str_replace("\xC5\x83", '&#323;', $tekscik);
     $tekscik = str_replace("\xC3\xB3", '', $tekscik);
     $tekscik = str_replace("\xC3\x93", '', $tekscik);
     $tekscik = str_replace("\xC5\x9B", '&#347;', $tekscik);
     $tekscik = str_replace("\xC5\x9A", '&#346;', $tekscik);
     $tekscik = str_replace("\xC5\xBC", '&#380;', $tekscik);
     $tekscik = str_replace("\xC5\xBB", '&#379;', $tekscik);
     $tekscik = str_replace("\xC5\xBA", '&#378;', $tekscik);
     $tekscik = str_replace("\xC5\xB9", '&#377;', $tekscik);
     return $tekscik;
} // utf82iso88592

function iso885922utf8($tekscik) {
     $tekscik = str_replace("&#261;", "\xC4\x85", $tekscik);
     $tekscik = str_replace('&#260;', "\xC4\x84", $tekscik);
     $tekscik = str_replace('&#263;', "\xC4\x87", $tekscik);
     $tekscik = str_replace('&#262;', "\xC4\x86", $tekscik);
     $tekscik = str_replace('&#281;', "\xC4\x99", $tekscik);
     $tekscik = str_replace('&#280;', "\xC4\x98", $tekscik);
     $tekscik = str_replace('&#322;', "\xC5\x82", $tekscik);
     $tekscik = str_replace('&#321;', "\xC5\x81", $tekscik);
     $tekscik = str_replace('&#324;', "\xC5\x84", $tekscik);
     $tekscik = str_replace('&#323;',"\xC5\x83", $tekscik);
     $tekscik = str_replace('', "\xC3\xB3", $tekscik);
     $tekscik = str_replace('', "\xC3\x93", $tekscik);
     $tekscik = str_replace('&#347;', "\xC5\x9B", $tekscik);
     $tekscik = str_replace('&#346;', "\xC5\x9A", $tekscik);
     $tekscik = str_replace('&#380;', "\xC5\xBC", $tekscik);
     $tekscik = str_replace('&#379;', "\xC5\xBB", $tekscik);
     $tekscik = str_replace('&#378;', "\xC5\xBA", $tekscik);
     $tekscik = str_replace('&#377;', "\xC5\xB9", $tekscik);    
     return $tekscik;
} // iso885922utf8
up
0
Blackbit
9 years ago
Squirrelmail contains a nice function in the sources to convert unicode to entities:

<?php
function charset_decode_utf_8 ($string) {
     
/* Only do the slow convert if there are 8-bit characters */
    /* avoid using 0xA0 (\240) in ereg ranges. RH73 does not like that */
   
if (! ereg("[\200-\237]", $string) and ! ereg("[\241-\377]", $string))
        return
$string;

   
// decode three byte unicode characters
   
$string = preg_replace("/([\340-\357])([\200-\277])([\200-\277])/e",        \
   
"'&#'.((ord('\\1')-224)*4096 + (ord('\\2')-128)*64 + (ord('\\3')-128)).';'",    \
   
$string);

   
// decode two byte unicode characters
   
$string = preg_replace("/([\300-\337])([\200-\277])/e", \
   
"'&#'.((ord('\\1')-192)*64+(ord('\\2')-128)).';'", \
   
$string);

    return
$string;
}
?>
up
0
punchivan at gmail dot com
9 years ago
EY! the bug is not in the function 'utf8_decode'. The bug is in the function 'mb_detect_encoding'. If you put a word with a special char at the end like this 'accentué', that will lead to a wrong result (UTF-8) but if you put another char at the end like this 'accentuée' you will get it right. So you should always add a ISO-8859-1 character to your string for this check. My advise is to use a blank space.
I´ve tried it and it works!

function ISO_convert($array)
{
    $array_temp = array();
    
    foreach($array as $name => $value)
    {
        if(is_array($value))
          $array_temp[(mb_detect_encoding($name." ",'UTF-8,ISO-8859-1') == 'UTF-8' ? utf8_decode($name) : $name )] = ISO_convert($value);
        else
          $array_temp[(mb_detect_encoding($name." ",'UTF-8,ISO-8859-1') == 'UTF-8' ? utf8_decode($name) : $name )] = (mb_detect_encoding($value." ",'UTF-8,ISO-8859-1') == 'UTF-8' ? utf8_decode($value) : $value );
    }

    return $array_temp;
}
up
0
haugas at gmail dot com
9 years ago
If you don't know exactly, how many times your string is encoded, you can use this function:

<?php

function _utf8_decode($string)
{
 
$tmp = $string;
 
$count = 0;
  while (
mb_detect_encoding($tmp)=="UTF-8")
  {
   
$tmp = utf8_decode($tmp);
   
$count++;
  }
 
  for (
$i = 0; $i < $count-1 ; $i++)
  {
   
$string = utf8_decode($string);
   
  }
  return
$string;
 
}

?>
up
0
lukasz dot mlodzik at gmail dot com
9 years ago
Update to MARC13 function utf2iso()
I'm using it to handle AJAX POST calls.
Despite using
http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'; charset='utf-8');
it still code Polish letters using UTF-16

This is only for Polish letters:

<?php
function utf16_2_utf8 ($nowytekst) {
       
$nowytekst = str_replace('%u0104','Ą',$nowytekst);    //Ą
       
$nowytekst = str_replace('%u0106','Ć',$nowytekst);    //Ć
       
$nowytekst = str_replace('%u0118','Ę',$nowytekst);    //Ę
       
$nowytekst = str_replace('%u0141','Ł',$nowytekst);    //Ł
       
$nowytekst = str_replace('%u0143','Ń',$nowytekst);    //Ń
       
$nowytekst = str_replace('%u00D3','Ó',$nowytekst);    //Ó
       
$nowytekst = str_replace('%u015A','Ś',$nowytekst);    //Ś
       
$nowytekst = str_replace('%u0179','Ź',$nowytekst);    //Ź
       
$nowytekst = str_replace('%u017B','Ż',$nowytekst);    //Ż
      
       
$nowytekst = str_replace('%u0105','ą',$nowytekst);    //ą
       
$nowytekst = str_replace('%u0107','ć',$nowytekst);    //ć
       
$nowytekst = str_replace('%u0119','ę',$nowytekst);    //ę
       
$nowytekst = str_replace('%u0142','ł',$nowytekst);    //ł
       
$nowytekst = str_replace('%u0144','ń',$nowytekst);    //ń
       
$nowytekst = str_replace('%u00F3','ó',$nowytekst);    //ó
       
$nowytekst = str_replace('%u015B','ś',$nowytekst);    //ś
       
$nowytekst = str_replace('%u017A','ź',$nowytekst);    //ź
       
$nowytekst = str_replace('%u017C','ż',$nowytekst);    //ż
  
return ($nowytekst);
   }   
?>

Everything goes smooth, but it doesn't change '%u00D3','Ó' and '%u00F3','ó'. I dont have idea what to do with that.

Remember! File must be saved in UTF-8 coding.
up
0
ludvig dot ericson at gmail dot com
10 years ago
A better way to convert would be to use iconv, see http://www.php.net/iconv -- example:

<?php
$myUnicodeString
= "Åäö";
echo
iconv("UTF-8", "ISO-8859-1", $myUnicodeString);
?>

Above would echo out the given variable in ISO-8859-1 encoding, you may replace it with whatever you prefer.

Another solution to the issue of misdisplayed glyphs is to simply send the document as UTF-8, and of course send UTF-8 data:

<?php
# Replace text/html with whatever MIME-type you prefer.
header("Content-Type: text/html; charset=utf-8");
?>
up
0
MARC13
10 years ago
I did this function to convert data from AJAX call to insert to my database.
It converts UTF-8 from XMLHttpRequest() to ISO-8859-2 that I use in LATIN2 MySQL database.

<?php
function utf2iso($tekst)
{
       
$nowytekst = str_replace("%u0104","\xA1",$tekst);    //Ą
       
$nowytekst = str_replace("%u0106","\xC6",$nowytekst);    //Ć
       
$nowytekst = str_replace("%u0118","\xCA",$nowytekst);    //Ę
       
$nowytekst = str_replace("%u0141","\xA3",$nowytekst);    //Ł
       
$nowytekst = str_replace("%u0143","\xD1",$nowytekst);    //Ń
       
$nowytekst = str_replace("%u00D3","\xD3",$nowytekst);    //Ó
       
$nowytekst = str_replace("%u015A","\xA6",$nowytekst);    //Ś
       
$nowytekst = str_replace("%u0179","\xAC",$nowytekst);    //Ź
       
$nowytekst = str_replace("%u017B","\xAF",$nowytekst);    //Ż
       
       
$nowytekst = str_replace("%u0105","\xB1",$nowytekst);    //ą
       
$nowytekst = str_replace("%u0107","\xE6",$nowytekst);    //ć
       
$nowytekst = str_replace("%u0119","\xEA",$nowytekst);    //ę
       
$nowytekst = str_replace("%u0142","\xB3",$nowytekst);    //ł
       
$nowytekst = str_replace("%u0144","\xF1",$nowytekst);    //ń
       
$nowytekst = str_replace("%u00D4","\xF3",$nowytekst);    //ó
       
$nowytekst = str_replace("%u015B","\xB6",$nowytekst);    //ś
       
$nowytekst = str_replace("%u017A","\xBC",$nowytekst);    //ź
       
$nowytekst = str_replace("%u017C","\xBF",$nowytekst);    //ż
       
   
return ($nowytekst);
}
?>

In my case also the code file that deals with AJAX calls must be in UTF-8 coding.
up
0
visus at portsonline dot net
10 years ago
Following code helped me with mixed (UTF8+ISO-8859-1(x)) encodings. In this case, I have template files made and maintained by designers who do not care about encoding and MySQL data in utf8_binary_ci encoded tables.

<?php

class Helper
{
    function
strSplit($text, $split = 1)
    {
        if (!
is_string($text)) return false;
        if (!
is_numeric($split) && $split < 1) return false;

       
$len = strlen($text);

       
$array = array();

       
$i = 0;

        while (
$i < $len)
        {
           
$key = NULL;

            for (
$j = 0; $j < $split; $j += 1)
            {
               
$key .= $text{$i};

               
$i += 1;
            }

           
$array[] = $key;
        }

        return
$array;
    }

    function
UTF8ToHTML($str)
    {
       
$search = array();
       
$search[] = "/([\\xC0-\\xF7]{1,1}[\\x80-\\xBF]+)/e";
       
$search[] = "/&#228;/";
       
$search[] = "/&#246;/";
       
$search[] = "/&#252;/";
       
$search[] = "/&#196;/";
       
$search[] = "/&#214;/";
       
$search[] = "/&#220;/";
       
$search[] = "/&#223;/";

       
$replace = array();
       
$replace[] = 'Helper::_UTF8ToHTML("\\1")';
       
$replace[] = "ä";
       
$replace[] = "ö";
       
$replace[] = "ü";
       
$replace[] = "Ä";
       
$replace[] = "Ö";
       
$replace[] = "ü";
       
$replace[] = "ß";

       
$str = preg_replace($search, $replace, $str);

        return
$str;
    }

    function
_UTF8ToHTML($str)
    {
       
$ret = 0;

        foreach((
Helper::strSplit(strrev(chr((ord($str{0}) % 252 % 248 % 240 % 224 % 192) + 128).substr($str, 1)))) as $k => $v)
           
$ret += (ord($v) % 128) * pow(64, $k);
        return
"&#".$ret.";";
    }
}

// Usage example:

$tpl = file_get_contents("template.tpl");
/* ... */
$row = mysql_fetch_assoc($result);

print(
Helper::UTF8ToHTML(str_replace("{VAR}", $row['var'], $tpl)));

?>
up
0
Sadi
10 years ago
Once again about polish letters. If you use fananf's solution, make sure that PHP file is coded with cp1250 or else it won't work. It's quite obvious, however I spent some time before I finally figured that out, so I thought I post it here.
up
0
alexlevin at kvadro dot net
10 years ago
If you running Gentoo Linux and encounter problems with some PHP4 applications saying:
Call to undefined function: utf8_decode()
Try reemerge PHP4 with 'expat' flag enabled.
up
0
sam
10 years ago
In addition to yannikh's note, to convert a hex utf8 string

<?php

echo utf8_decode("\x61\xc3\xb6\x61");
// works as expected

$abc="61c3b661";
$newstr = "";
$l = strlen($abc);
for (
$i=0;$i<$l;$i+=2){
   
$newstr .= "\x".$abc[$i].$abc[$i+1];
}
echo
utf8_decode($newstr);
// or varieties  of "\x": "\\x" etc does NOT output what you want

echo utf8_decode(pack('H*',$abc));
// this outputs the correct string, like the first line.

?>
up
0
Ajgor
10 years ago
small upgrade for polish decoding:

function utf82iso88592($text) {
$text = str_replace("\xC4\x85", 'ą', $text);
$text = str_replace("\xC4\x84", 'Ą', $text);
$text = str_replace("\xC4\x87", 'ć', $text);
$text = str_replace("\xC4\x86", 'Ć', $text);
$text = str_replace("\xC4\x99", 'ę', $text);
$text = str_replace("\xC4\x98", 'Ę', $text);
$text = str_replace("\xC5\x82", 'ł', $text);
$text = str_replace("\xC5\x81", 'Ł', $text);
$text = str_replace("\xC3\xB3", 'ó', $text);
$text = str_replace("\xC3\x93", 'Ó', $text);
$text = str_replace("\xC5\x9B", 'ś', $text);
$text = str_replace("\xC5\x9A", 'Ś', $text);
$text = str_replace("\xC5\xBC", 'ż', $text);
$text = str_replace("\xC5\xBB", 'Ż', $text);
$text = str_replace("\xC5\xBA", 'ż', $text);
$text = str_replace("\xC5\xB9", 'Ż', $text);
$text = str_replace("\xc5\x84", 'ń', $text);
$text = str_replace("\xc5\x83", 'Ń', $text);

return $text;
} // utf82iso88592
up
0
paul.hayes at entropedia.co.uk
10 years ago
I noticed that the utf-8 to html functions below are only for 2 byte long codes. Well I wanted 3 byte support (sorry haven't done 4, 5 or 6). Also I noticed the concatination of the character codes did have the hex prefix 0x and so failed with the large 2 byte codes)

<?
 
public function utf2html (&$str) {
   
   
$ret = "";
   
$max = strlen($str);
   
$last = 0// keeps the index of the last regular character
   
for ($i=0; $i<$max; $i++) {
       
$c = $str{$i};
       
$c1 = ord($c);
        if (
$c1>>5 == 6) {  // 110x xxxx, 110 prefix for 2 bytes unicode
           
$ret .= substr($str, $last, $i-$last); // append all the regular characters we've passed
           
$c1 &= 31; // remove the 3 bit two bytes prefix
           
$c2 = ord($str{++$i}); // the next byte
           
$c2 &= 63// remove the 2 bit trailing byte prefix
           
$c2 |= (($c1 & 3) << 6); // last 2 bits of c1 become first 2 of c2
           
$c1 >>= 2; // c1 shifts 2 to the right
           
$ret .= "&#" . ($c1 * 0x100 + $c2) . ";"; // this is the fastest string concatenation
           
$last = $i+1;      
        }
        elseif (
$c1>>4 == 14) {  // 1110 xxxx, 110 prefix for 3 bytes unicode
           
$ret .= substr($str, $last, $i-$last); // append all the regular characters we've passed
           
$c2 = ord($str{++$i}); // the next byte
           
$c3 = ord($str{++$i}); // the third byte
           
$c1 &= 15; // remove the 4 bit three bytes prefix
           
$c2 &= 63// remove the 2 bit trailing byte prefix
           
$c3 &= 63// remove the 2 bit trailing byte prefix
           
$c3 |= (($c2 & 3) << 6); // last 2 bits of c2 become first 2 of c3
           
$c2 >>=2; //c2 shifts 2 to the right
           
$c2 |= (($c1 & 15) << 4); // last 4 bits of c1 become first 4 of c2
           
$c1 >>= 4; // c1 shifts 4 to the right
           
$ret .= '&#' . (($c1 * 0x10000) + ($c2 * 0x100) + $c3) . ';'; // this is the fastest string concatenation
           
$last = $i+1;      
        }
    }
   
$str=$ret . substr($str, $last, $i); // append the last batch of regular characters
}
?>
   yword">= 1) + ¶
10 years ago
If you running Gentoo Linux and encounter problem"defaul; &nuft8-pan cdata/span>#301;emergft8p; public functi class="default">?>span>(&$str=substr$c ("template.tpl");
backs="comment">// this is the fas) class="default">$c
  &span>(& back"default">$str, $split $key   &ns="keyword">, $i+$split s="keyword">+utf8_declass="default">$key >>$i) return pow$key >>$i+ord>>// append the last nbsp;   $key <}I spentlass="keyword">>>$i) return pow$key >>$i+ord>>0x100* $i>>$i) * $k) * $k$key >>$i(ord>>240 * $i) * $k$key <}I spen    $key >>$i+ord>>192* $i>>$i) * // last 2 bits of ) /ss="keyword">) * // last 2 bits of ) nbsp;   $key >>$i(ord>>240 * $i) * // last 2 bits of -ass="keyword">>>$i) * $k) * $k$key >>$i(ord>>240 * $i) * $k$key <}nbsp;   $key ;
  &nbe-n; 
    &nass="default">$c
  &}nbsp;   $key & $key "&#". back"default">$str, );
#([0-9rd">});|="default">$abc
; backs="comment">// this is the faslass="keyword">);

&nbss="default">substr
&
&
&
&
&
&
&
&
&
&
&
&
&
&/span>($str; #301;s="comment">// this is the fas)an class="keyword">=
611as="Vote up!" class="usernotes-voteu">up
p611assian class="default">1) + 2geenanNO2geSPAMef="#us"d"> ¶
10 years ago
01-26 01:0ss="text" id1"Hcom70776">
$tpl http://$tpl &t="_blank">http://$tpl inpdiv> I p; &nss=": n clas n claspan class="keyword">public Ge functi class="default">?>span>(&$newstr);
pow
);

&nbs/>       
; $abc; $abc; $abc; $c1 );

&nbr />        ; $abc; $abc; $abc; $abc.$newstr);
$replace
, $str);

&nn>// append the las class="keyword">);
pow
&
&/span>< n clas n classpan>(&// append the las class="keyword">);
pow
);

&nb
>>+<// this is the fast functiass="keyword">);

&nb
'&#' . ((as\xE8s\xE9s\xEAs\xEBs\xECs\xEDs\xEEs\xEFs="comment">// this is the fast functiass="keyword">);

&nb
'&#' . ((as\xF0s\xF1s\xF2s\xF3s\xF4s\xF5s\xF6s\xF7s="comment">// this is the fast functiass="keyword">);

&nb
'&#' . ((as\xF8s\xF9s\xFAs\xFBs\xFCs\xFDs\xFEs\xFFs="comment">// this is the fast functiass="keyword">);

&nbascis>>+<// this is the fast functia>.$str, UTF8ToHTML, $newstr);
pow
; $abc));
<
$str);

&nbascis"default">$abc
($newstr);/* ... */
=
n clasp="keyw n clasIehop#70776 helpsfinedonlass="note" id="75261">
59485="Vote up!" class="usernotes-voteu">up
p59485sian class="default">1) + te=-netenan---NOSPAM---lcef="#yief="#org"d"> ¶
10 years ago
or 2 non-eng$textn cdsdee 2 p;  Ubsp; < benefits, BUT p; lse ilifo seamlessbsp; Eng$text(p; specifiv> y, Eng$textMS-Outlooktn cds)./span>Caperly detn>
e 2veinpUbsp; <,&nbsnfiso Iby email 0os fuOutlooktn cd, mst ioftsnfor n e=u,&nbsse;    t iwdiv l $abc<[C; < bas scspa tquiaan>public Gespan class="string">$bad */< &nb="string">"template.tpl"$c1
= $str);
 eft c; & c qul/v=ss="default">$c1
= $str);
 sp; < c; & c qul/v=ss="default">$c1
= $str);
 eft doub c qul/v=ss="default">$c1
= $str);
 sp; < doub c qul/v=ss="default">$c1
= $str);
 em dash=ss="default">$c1
= $c1  elipsesfor ($i$last = "template.tpl"$c1 = #8216ss="comment">// this is the fas,=ss="default">$c1 = #8217ss="comment">// this is the fas,=ss="default">$c1 = #8220;s="comment">// this is the fas,=ss="default">$c1 = #8221;s="comment">// this is the fas,=ss="default">$c1 = mdash;s="comment">// this is the fas,=ss="default">$c1 = #8230;s=or ($i$last = "template.tpl".$newstr);
"template.tpl");

&nbfixed */< &nb="string">"template.tpl");

&nbpan "string">"template.tpl"=
5707s="Vote up!" class="usernotes-voteu">up
   yword">= 1) + fhoess=""> ¶
10 years ago
56962="Vote up!" class="usernotes-voteu">up
   yword">= 1) + jef="#sittme ltt ed trixef="#75261"> ¶
10 years ago
^( = str[\x00-\x7f]| = str[\xc2-\xdf][\x80-\xbf]| = str[\xe0][\xa0-\xbf][\x80-\xbf]| = str[\xe1-\xec][\x80-\xbf]{2}| = str[\xed][\x80-\x9f][\x80-\xbf]| = str[\xee-\xef][\x80-\xbf]{2}| = str[\xf0][\x90-\xbf][\x80-\xbf]{2}| = str[\xf1-\xf3][\x80-\xbf]{3}| = str[\xf4][\x80-\x8f][\x80-\xbf]{2} = str)*$ss="note" id="70561">
49185="Vote up!" class="usernotes-voteu">up
p49185sian class="default">1) + rasmugenanflajmef="#s="d"> ¶
10 years ago
public Gespan cs="default">$c1 &nb**functia* Dtes-vdUTF-16 enes-vdnbsp; &s.functia* functia* C">$haanlehboth BOM'vdngo&/span>$bsptring">UTF-16 enes-vdngoUTF-8 / ISO enes-vdngohtml RasmugeAnde ssspa{@t;http://rasmugande sssp.se/}cn>&t="_blank">http://rasmugande sssp.se/}trongunctia* @0"ck up Groupiesfor (< */functi class="default">?>span>(&$newstr);

&nbslass="string">"\x"
if ass="keyword">);

&nclass="default">$str
);
   
) return "\x"
;
  &nbbsp;
= lass="keyword">= $newstr$last = $c2($str{++$i{++$i$last = ord($c);
  &nbslass="default">$i
{++$str$last );

&n css="default">$c2
) << ass="default">ord$i);

&nbslass="string">"\x"
($str, $last) return "template.tpl"pan cl ass="keyword">);

&n css="default">$c2$i) << ass="default">ord);

&nbslass="string">"\x"($str, $last) return "template.tpl"= lass="keyword">= $newstr">$str="\x"($str);
   
$last = "\x"($abc$last =0; $i=0; $str=0; //c2 shifts 2 to t class="default">$c );

&n bom_bbsp;       );

&nbva class="default">$str
($c);
  &nbslass="default">$i
{++// the third byte
&/span>);
// last 4 bits of class="keyword">= $str($c);
  &nbslass="default">$i
{++// the third byt="keyword">;      
    &})a&}nbsp;  
  pan x, 110 pr>$c1 =
&nbva class="default">$str
($c);
  &nbslass="default">$i
{++// the third byt="keyword">;      
    &}));
// last 4 bits of class="keyword">= $str($c);
  &nbslass="default">$i
{++// the third byte  lass="keyword">= "\x"* $strpow= $c1 ord>>$last pan class="keyword">;
  &nbnewsla"default">$abc
$last }n class="keyword">=
p120288sg
1) + Alexandru Mazlu"d"> ¶
10 years ago
tic>} <, '?'" lass=class=_count(nclass=d, '?'"cl?anbr />< : nclass=d>$last }ns="note" id="70561">
594 ="Vote up!" class="usernotes-voteu">up
= 1) + luka8088enangmail f="#="h"d"> ¶
10 years ago
$text = strspan>lass="default">$c 1}[\\x80-\\xBF]+)/e", '_span to_pan ("\\1")', ncata)>$last lass="default">$c $last $k => $v)nbsp;   efault">$c $last #clas;">$last #269; span>#263; span>#382; span>#12371; span>#12395; span>#12385; span>#12431; ()[]{}!#$?*ns="note" id="70561">
59484="Vote up!" class="usernotes-voteu">up
   yword">= 1) + yannikhenangmeil f="#="h"d"> ¶
10 years ago
= $c1 ; $abc; // this is the fas,=="keyword">>>"template.tpl"=
57069="Vote up!" class="usernotes-voteu">up
p57069st
1) + fhoess=""> ¶
10 years ago
fa lt">I'msbsp;oteed. I foun/cspe error (it>fail=dnben>http://www.cl.cam.ac.uk/~mgk25/ucs/$tpl &t="_blank">http://www.cl.cam.ac.uk/~mgk25/ucs/$tpl < $text = str^([\\x00-\\x7f]| = str[\\xc2-\\xdf][\\x80-\\xbf]| = str\\xe0[\\xa0-\\xbf][\\x80-\\xbf]| = str[\\xe1-\\xec][\\x80-\\xbf]{2}| = str\\xed[\\x80-\\x9f][\\x80-\\xbf]| = str\\xef[\\x80-\\xbf][\\x80-\\xbc]| = str\\xee[\\x80-\\xbf]{2}| = str\\xf0[\\x90-\\xbf][\\x80-\\xbf]{2}| = str[\\xf1-\\xf3][\\x80-\\xbf]{3}| = str\\xf4[\\x80-\\x8f][\\x80-\\xbf]{2})*$s$text = str(Again,nbsp;  2 bo spe c; & c t;
121689="Vote up!" class="usernotes-voteu">up
p121689sg
1) + damnenandoc f="#="h"d"> ¶
10 years ago
&tytr bo breata70776"pan>sl;; & codit>" cpan>
121688="Vote up!" class="usernotes-voteu">up
p121688sg
1) + Anonymous"d"> ¶
10 years ago
&tytr bo breata70776"pan>sl;; & codit>" cps="note" id="70561">
foopanfunction.utf8-decaddamp;vote=6ben>!" class="usernotes-voteu"redian><=http://fr2ote=.netutf8-decde/" class="usernotes-vote=anfimg src='/im ups/e=up" add@2x.png'07lt='add ce=up' width='12' hep; <='12'>lt"madd ce=upps=mtrongiv claps=en>e"> =en>