note 38591 added to function.utf8-decode

From: Date: Mon, 29 Dec 2003 02:40:24 +0000
Subject: note 38591 added to function.utf8-decode
Groups: php.notes 
Request: Send a blank email to php-notes+get-62622@lists.php.net to get a copy of this message
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!) ---- Manual Page -- http://www.php.net/manual/en/function.utf8-decode.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+38591 Delete -- http://master.php.net/manage/user-notes.php?action=delete+38591&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+38591&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#62622) next »