Jump to content

Input Form + umlauts + strtr


beerguide_au

Recommended Posts

I have an input form which needs to be able to handle umlauts (eg: ä ö etc..) and passes them via POST to a php file I have created. In my php file I have the following code:

 

// Convert accents and extended characters into english equivalents.
$characters = array( "'" => "", "&" => "And", "(" => "", ")" => "", "-" => " " );
$umlauts = array( "ä" => "ae", "ö" => "oe", "ü" => "ue", "ß" => "ss" );
$replacements = array_merge( $characters, $umlauts );
$beer_name = strtr( strip_tags( trim( $_POST['beer_name'] ) ), $replacements );

 

In my header I have the following code to allow/force utf-8:

 

// Setting the Content-Type header with charset
header('Content-Type: text/html; charset=utf-8');

 

When I try to echo any content, or use it in a mysql table the $characters part is correct and is converting things like the & to And, but the umlauts section is ignored and ä etc.. is echo'd to the screen instead of the converted string, in this case 'ae'. This leads me to think it has something to do with either how the form is handling the POST content and/or I'm using an incorrect character set?

 

Can anyone please help me with this issue? It would be much appreciated.

Link to comment
Share on other sites

There's a lot of messy character set issues here.. first thing to do is just to echo $_POST['beer_name'] and see what you get.

 

Make sure that you look at the source of the page rather than the rendered output (go to View -> View Source or the equivalent in your browser).  There are many different ways of encoding a character, and if the encodings don't match exactly, the strtr() will not work.

 

I suspect your problem is that the characters have been urlencoded, as in the functions urlencode() and urldecode().

Link to comment
Share on other sites

Thank you for the speedy reply.

I have viewed the source (im using IE 7) and I have the following:

Test & Weißbier<br /> (Noting the ß is displayed like that in the source, as entered into the form originally)

 

I too have a feeling it is a matching issue as the $characters array works, so the function strtr is working for non $umlauts entities.

 

Is there a way I can display exactly what the form is seeing the POST other than 'viewing html source' through a browser?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.