note 38591 deleted from function.utf8-decode by cmb
| From: | cmb@php.net | Date: | Sun, 11 Dec 2016 11:29:40 +0000 |
| Subject: | note 38591 deleted from function.utf8-decode by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208524@lists.php.net to get a copy of this message | ||
Note Submitter: lg83news at free dot fr
----
Here are some functions for converting UTF-8 to and from Latin 9 (aka ISO-8859-15), if your PHP file
is encoded as UTF-8.
<?php
function latin9_to_utf8($latin9str) { // replaces utf8_encode()
$trans = array("¤"=>"", "¦"=>"",
"¨"=>"", "´"=>"",
"¸"=>"", "¼"=>"",
"½"=>"", "¾"=>"");
$wrong_utf8str = utf8_encode($latin9str);
$utf8str = strtr($wrong_utf8str, $trans);
return $utf8str;
}
function utf8_to_latin9($utf8str) { // replaces utf8_decode()
$trans = array(""=>"¤", ""=>"¦",
""=>"¨", ""=>"´",
""=>"¸", ""=>"¼",
""=>"½", ""=>"¾");
$wrong_utf8str = strtr($utf8str, $trans);
$latin9str = utf8_decode($wrong_utf8str);
return $latin9str;
}
?>
Note: the above functions will not work if your PHP file is not encoded in UTF-8.
You can copy this binary data to your PHP file instead, it'll work with any encoding (including
UTF-8) as long as you copy it as bytes, not characters:
<?php
function latin9_to_utf8($latin9str) { // replaces utf8_encode()
$trans = array("¤"=>"â¬", "¦"=>"Å ",
"¨"=>"š", "´"=>"Ž",
"¸"=>"ž", "¼"=>"Å",
"½"=>"Å", "¾"=>"Ÿ");
$wrong_utf8str = utf8_encode($latin9str);
$utf8str = strtr($wrong_utf8str, $trans);
return $utf8str;
}
function utf8_to_latin9($utf8str) { // replaces utf8_decode()
$trans = array("â¬"=>"¤", "Å "=>"¦",
"š"=>"¨", "Ž"=>"´",
"ž"=>"¸", "Å"=>"¼",
"Å"=>"½", "Ÿ"=>"¾");
$wrong_utf8str = strtr($utf8str, $trans);
$latin9str = utf8_decode($wrong_utf8str);
return $latin9str;
}
?>
(Note: the PHP manual is served as ISO-8859-1, but many of the bytes used here have no meaning in
ISO-8859-1, so they'll probably be displayed as Windows-1252 if your using Windows, or garbage
otherwise. Just copy the bytes!)