homepage Welcome to WebmasterWorld Guest from 54.167.11.16
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Submitting foreign language characters in myqsl from form
optik




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

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.

 

ZakAltF4




msg:4412396
 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..

optik




msg:4412502
 11:07 pm on Jan 30, 2012 (gmt 0)

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.

ZakAltF4




msg:4412519
 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.

enigma1




msg:4412641
 11:57 am on Jan 31, 2012 (gmt 0)

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.

henry0




msg:4413565
 4:14 pm on Feb 2, 2012 (gmt 0)

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!

optik




msg:4413928
 3:20 pm on Feb 3, 2012 (gmt 0)

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.

enigma1




msg:4413952
 5:24 pm on Feb 3, 2012 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved