Page 1 of 1
decode Aññü Mäöph ?
Posted: Wed Jan 07, 2004 4:42 am
by webweber
I'm exchanging data between a Flash file and a mySQL database through PHP.
If the user enters something with special characters like 'Añña Müllör' this string ends up in the DB like 'Aññü Mäöph'.
How can I decode this monster?
Thanks in advance!
Andreas Weber
motiondraw.com
Posted: Wed Jan 07, 2004 4:55 am
by m3mn0n
[php_man]str_replace[/php_man](), [php_man]foreach[/php_man](), and an [php_man]array[/php_man]() of predefined characters.
Posted: Wed Jan 07, 2004 8:28 am
by webweber
Sami,
Thanks for your reply!
... even though I would have hoped that there would be an easier solution (Flash decodes these monsters automatically, the special characters only give me problems when I want to use the values in PHP, e.g. for sending them in a mail)
Just for the archives how I got it working:
Digging on php.net I found the following function (which I had to extend a bit to get rid of the htmlentities):
Code: Select all
//decodes Aññü Mäöph to something like Añña Müölph
function crossUrlDecode($source) {
$decodedStr = '';
$pos = 0;
$len = strlen($source);
while ($pos < $len) {
$charAt = substr ($source, $pos, 1);
if ($charAt == 'Ã') {
$char2 = substr($source, $pos, 2);
$decodedStr .= htmlentities(utf8_decode($char2),ENT_QUOTES,'ISO-8859-1');
$pos += 2;
}
elseif(ord($charAt) > 127) {
$decodedStr .= "&#".ord($charAt).";";
$pos++;
}
elseif($charAt == '%') {
$pos++;
$hex2 = substr($source, $pos, 2);
$dechex = chr(hexdec($hex2));
if($dechex == 'Ã') {
$pos += 2;
if(substr($source, $pos, 1) == '%') {
$pos++;
$char2a = chr(hexdec(substr($source, $pos, 2)));
$decodedStr .= htmlentities(utf8_decode($dechex . $char2a),ENT_QUOTES,'ISO-8859-1');
}
else {
$decodedStr .= htmlentities(utf8_decode($dechex));
}
}
else {
$decodedStr .= $dechex;
}
$pos += 2;
}
else {
$decodedStr .= $charAt;
$pos++;
}
}
return unhtmlentities($decodedStr);
}
function unhtmlentities($string) {
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
$trans_tbl = array_flip($trans_tbl);
return strtr($string, $trans_tbl);
}
Posted: Wed Jan 07, 2004 9:10 am
by malcolmboston
couldnt you just not allow them characters and sami said
just replace a ñ with n etc?
Posted: Wed Jan 07, 2004 9:35 am
by webweber
Maybe I misunderstand you, but are you suggesting that if a user's name is e.g. Müller his name should be changed to Muller or Mueller?
This specific site is used by a lot of users from non-anglosaxon countries. And of course there must be a way to register them with their exact names.
I'm really glad I found this crossUrlDecode() function. I wouldn't have known where to start with the 'predefined characters' - which are the special characters in scandinavian languages like Norwegian or Islandic?
I'm still puzzled that there isn't a built-in PHP function to handle this problem.