Welcome to WebmasterWorld Guest from 54.211.86.24

Forum Moderators: coopster & jatar k

Submitting foreign language characters in myqsl from form

   
1:12 pm on Jan 27, 2012 (gmt 0)

5+ Year Member



If I directly add München to my database it's fine but when inserting from a form the umlauted U gets replaced by other letters.
5:48 pm on Jan 30, 2012 (gmt 0)



I would use ASCII for special characters. Use a str replace in your php... Something like



string that needs to be customized
$string = "blah blah blah München";


$r_string = str_replace("Ü", "Ü", $string);
$r_string = str_replace("ü", "ü", $string);



I use this method for storing a lot of special characters in MySQL..
11:07 pm on Jan 30, 2012 (gmt 0)

5+ Year Member



There a lot of special foreign characters though, I don't really want to pass every SQL insert through a mass of str_replace functions.
12:33 am on Jan 31, 2012 (gmt 0)



This is the code I use:

I use this to prepare my string to be "clean"

function clean_for_mysql($string,$max_length) {
$in_string = ltrim($string);
$in_string = rtrim($in_string);
if (round($max_length) < 1) {
$max_length = 131072; // 128K
}
if (strlen($in_string) > $max_length) {
$new_string = substr($in_string,0,$max_length);
}
$new_string = mysql_real_escape_string($new_string);
return $new_string;
}


Then I use str replace with arrays:

$string = "Your String";
$search = array('€', 'ƒ', 'Ö', 'Ü');
$replace = array('&euro;', '&#131;', '&#214;', '&#220;');
echo str_replace($search, $replace, $string);


How many special characters are you REALLY expecting? ... My list is about 50-60. This was semi time consuming, but not terribly so. Definitely something you could do in an afternoon.
11:57 am on Jan 31, 2012 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



And you shouldn't replace anything. Do you use a proper content-type with the form document? Is it UTF-8? Also make sure the content type is consistent with the database character set/collation you've setup and you're using.
4:14 pm on Feb 2, 2012 (gmt 0)

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Not to mention that sometimes MySQL will on its own replace the "&" in &#whatever; by the equivalent entity
thus rendering the content barely readable.
Did not find yet any way to avoid it!
3:20 pm on Feb 3, 2012 (gmt 0)

5+ Year Member



I agree enigma, the content-type of the page is set to UTF-8 and this is the same content-type PHPMyAdmin is using when it successfully submits these characters.
5:24 pm on Feb 3, 2012 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Ok when your script connects to the database do you setup the character set and the names? Also was the database created with utf8?
[dev.mysql.com...]

Basically when you add content to the db via your script forms, what you type is what you should see stored. You can dump the /POST array when the form is submitted and before the db write and see if the posted arguments look ok.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month