eddie Posted October 2, 2009 Posted October 2, 2009 Hello again I am having trouble converting special characters when a form is submitted. These include symbols like 'é'. I have tried using the php functions: htmlspecialchars and htmlentities, but these don't produce my desired result. Below are the two results of using the php functions above to convert 'Rosé Wine': Using htmlspecialchars $value = "Rosé Wine"; echo htmlspecialchars($value); // Output: Rosé Wine Using htmlentities $value = "Rosé Wine"; echo htmlentities($value); // Output: Rosé Wine Using str_replace $value = "Rosé Wine"; echo str_replace("é","é",$value); // Output: Rosé Wine As you can see this is not what i am looking for, below is the output that i am after. Using some magical php function that google cannot help me find $value = "Rosé Wine"; echo cleverfunction($value); // Output: Rosé Wine Also i would prefer not to have to create a custom made function as there maybe characters that i miss. Do you know how to do this? Thankyou very much!! Eddie
webman Posted October 2, 2009 Posted October 2, 2009 Hi Eddie Using htmlentities() does produce é for me. Have you tried specifying a character set in the third parameter? 1
eddie Posted October 2, 2009 Author Posted October 2, 2009 (edited) Hello, Thanks a lot, you were right! Just to clarify for anyone else who has this problem... I tried the top three character sets and nelow is the results: $value = "Rosé Wine"; echo htmlentities($value, ENT_QUOTES, "ISO8859-1"); // Output: Rosé Wine echo htmlentities($value, ENT_QUOTES, "ISO8859-15"); // Output: Rosé Wine echo htmlentities($value, ENT_QUOTES, "UTF-8"); // Output: Rosé Wine So you need to set the character set to UTF-8: echo htmlentities($value, ENT_QUOTES, "UTF-8"); Edited October 2, 2009 by eddie Easier to see the correct answer
webman Posted October 2, 2009 Posted October 2, 2009 Pleased you got it working It must be down to the character encoding of the string that is being passed to the function. I used AltGr+E to get the accent and pasted it into a text editor and ISO-8859-1 worked OK, whereas UTF-8 did nothing at all. In your case it's obviously different, but at least it works
Marci Posted October 6, 2009 Posted October 6, 2009 You can define UTF-8 for HTML content pagewide by dropping... ...into the header of all your pages, and ensuring when you create MySQL databases that you also specify UTF-8 charset. To make it the default charset for php, head to php.ini on your webserver, and look for... default_charset = ...and set it to UTF-8 also. But be prepared to change it back as it may break some session calls unless you reinstall the webapp that is calling them.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now