PHP convert foreign characters with accents

后端 未结 5 1273
有刺的猬
有刺的猬 2020-12-16 08:03

Hi I\'m trying to compare some text to the text in a database.. in the database any text with an accent is encoded like in html (ie. é) when I compare the databas

相关标签:
5条回答
  • 2020-12-16 08:25

    You need to send in the correct charset to htmlentities. It looks like you're using UTF-8, but the default is ISO-8859-1. Change it like this:

    $encoded = htmlentities($text, ENT_COMPAT, 'UTF-8');
    

    Another solution is to convert the text to ISO-8859-1 before encoding, but that may destroy information (ISO-8859-1 does not contain nearly as many characters as UTF-8). If you want to try that instead, do like this:

    $encoded = htmlentities(utf8_decode($text));
    
    0 讨论(0)
  • 2020-12-16 08:36

    Use simply as blow it works for Norwegian characters:

    function convert_accent($string)
    {
        return htmlspecialchars(utf8_decode($string));
    }
    
    0 讨论(0)
  • 2020-12-16 08:39

    The comparing task is related to the charset and the collation you selected when you create the database or the tables. If you are saving strings with a lot of accents like spanish I sugget you to use charset uft8 and the collation could be the more accurate to the language(english, french or whatever) you're using.

    The best thing of using the correct charset in the database is that you can save the string in natural way e.g: my name I can store it as is "Mario Juárez" and I have no need of doing some weird conversions.

    0 讨论(0)
  • 2020-12-16 08:39

    Ran into similar issues recently. Followed Emil's answer and it worked fine locally but not on our dev/stage environments. I ended up using this and it worked all around:

    $title = html_entity_decode(utf8_decode($item));
    

    Thanks for leading me in the right direction!

    0 讨论(0)
  • 2020-12-16 08:41

    I'm working on french site, and I also had same problem. This is the function that I use.

    function convert_accent($string)
    {
        return htmlspecialchars_decode(htmlentities(utf8_decode($string)));
    }
    

    What it does it decodes your string to utf8, than converts everything HTML entities. even tags. But we want to convert tags back to normal, than htmlspecialchars_decode will convert them back. So in the end you will get a string with converted accents without touching tags. You can use pass through this function your email content before sending it to recipent.

    Another issue you might face is that, sometimes with this function the content from database converts to ? . In this case you should do this before running your query:

    mysql_query("SET NAMES `utf8`");
    

    But you might need to do it, it depends on encoding in your table. I hope it helps.

    0 讨论(0)
提交回复
热议问题