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
decode Aññü Mäöph ?
Moderator: General Moderators
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):
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);
}-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
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.
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.