decode Aññü Mäöph ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
webweber
Forum Newbie
Posts: 7
Joined: Wed Aug 13, 2003 11:52 pm

decode Aññü Mäöph ?

Post 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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

[php_man]str_replace[/php_man](), [php_man]foreach[/php_man](), and an [php_man]array[/php_man]() of predefined characters.
webweber
Forum Newbie
Posts: 7
Joined: Wed Aug 13, 2003 11:52 pm

Post 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) &#123;
       $charAt = substr ($source, $pos, 1);
       if ($charAt == 'Ã') &#123;
           $char2 = substr($source, $pos, 2);
           $decodedStr .= htmlentities(utf8_decode($char2),ENT_QUOTES,'ISO-8859-1');
           $pos += 2;
       &#125;
       elseif(ord($charAt) > 127) &#123;
           $decodedStr .= "&#".ord($charAt).";";
           $pos++;
       &#125;
       elseif($charAt == '%') &#123;
           $pos++;
           $hex2 = substr($source, $pos, 2);
           $dechex = chr(hexdec($hex2));
           if($dechex == 'Ã') &#123;
               $pos += 2;
               if(substr($source, $pos, 1) == '%') &#123;
                   $pos++;
                   $char2a = chr(hexdec(substr($source, $pos, 2)));
                   $decodedStr .= htmlentities(utf8_decode($dechex . $char2a),ENT_QUOTES,'ISO-8859-1');
               &#125;
               else &#123;
                   $decodedStr .= htmlentities(utf8_decode($dechex));
               &#125;
           &#125;
           else &#123;
               $decodedStr .= $dechex;
           &#125;
           $pos += 2;
       &#125;
       else &#123;
           $decodedStr .= $charAt;
           $pos++;
       &#125;
   &#125;

   return unhtmlentities($decodedStr);
&#125; 

function unhtmlentities($string) &#123;
   $trans_tbl = get_html_translation_table(HTML_ENTITIES);
   $trans_tbl = array_flip($trans_tbl);
   return strtr($string, $trans_tbl);
&#125;
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

couldnt you just not allow them characters and sami said

just replace a ñ with n etc?
webweber
Forum Newbie
Posts: 7
Joined: Wed Aug 13, 2003 11:52 pm

Post 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.
Post Reply