+ Post New Thread
Results 1 to 5 of 5
Coding Thread, htmlspecialchars / htmlentities - Can't get to work in Coding and Web Development; Hello again I am having trouble converting special characters when a form is submitted. These include symbols like 'é'. I ...
  1. #1

    Join Date
    May 2009
    Posts
    8
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    htmlspecialchars / htmlentities - Can't get to work

    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

  2. IDG Tech News
  3. #2

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,334
    Blog Entries
    2
    Thank Post
    604
    Thanked 900 Times in 630 Posts
    Rep Power
    296
    Hi Eddie

    Using htmlentities() does produce é for me. Have you tried specifying a character set in the third parameter?

  4. Thanks to webman from:

    eddie (2nd October 2009)

  5. #3

    Join Date
    May 2009
    Posts
    8
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    [RESOLVED]

    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");
    Last edited by eddie; 2nd October 2009 at 12:22 PM. Reason: Easier to see the correct answer

  6. #4

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,334
    Blog Entries
    2
    Thank Post
    604
    Thanked 900 Times in 630 Posts
    Rep Power
    296
    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

  7. #5
    Marci's Avatar
    Join Date
    Jun 2008
    Location
    Wakefield, West Yorkshire
    Posts
    713
    Thank Post
    60
    Thanked 189 Times in 153 Posts
    Rep Power
    73
    You can define UTF-8 for HTML content pagewide by dropping...

    Code:
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    ...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...

    Code:
    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.

SHARE:
+ Post New Thread

Similar Threads

  1. Will it work?
    By tmcd35 in forum Wireless Networks
    Replies: 2
    Last Post: 8th January 2009, 11:30 PM
  2. "if you can't work with them, work round them"
    By djm968 in forum General Chat
    Replies: 45
    Last Post: 30th June 2008, 11:13 AM
  3. Let me in! I want to work!
    By m2d2 in forum General Chat
    Replies: 20
    Last Post: 21st December 2007, 11:00 PM
  4. For those with too much work to do..
    By beeswax in forum BETT 2013
    Replies: 5
    Last Post: 6th November 2007, 09:56 PM
  5. Could it ever work
    By ITWombat in forum Blue Skies
    Replies: 9
    Last Post: 15th July 2007, 01:20 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •