Page is a not externally linkable
httpwebwitch - 6:15 pm on Jul 13, 2010 (gmt 0)
here's an interesting illustration
<?php
header('Content-Type:text/html; charset=UTF-8');
$r = mysql_fetch_array(mysql_query("SELECT name FROM items WHERE itemid = 1234"));
$n = $r['name'];
echo $n;
// shows ? ?
$r = mysql_fetch_array(mysql_query("SELECT HEX(name) as name FROM items WHERE itemid = 1234"));
$n = $r['name'];
echo hex2str($n);
// shows chinese characters, properly
// hex2str is a nifty little function I found online:
function hex2str($hex){
$string='';
for ($i=0; $i < strlen($hex)-1; $i+=2){
$string .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $string;
}
?>
this method shows everything properly, regardless if it's chinese, spanish (with accents), or english (latin), in every browser I've tried.
It does seem like excessive effort, just to show some characters exactly as they're stored, from a UTF8 database onto a UTF8 page. Why does it work when I convert text into HEX and back again?