Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert foreign characters with accents

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 (i.e. é) when I compare the database text to my string it doesn't match because my string just shows é. When I use the PHP function htmlentities to encode the string first the é turns into é weird? Using htmlspecialchars doesn't encode the é at all.

How would you suggest I compare é to é as well as all the other accented characters?

like image 473
Devin Crossman Avatar asked Apr 25 '11 19:04

Devin Crossman


People also ask

How do you type special characters with accents?

The most reliable way to enter accented characters on a Windows PC is to use the Unicode Alt codes. Each accented character has its own 4-digit code. To enter a character, hold down the ALT key, type in the corresponding 4-digit code, and then release the ALT key.

How do you type in foreign accents?

CTRL+grave accent (the key to the left of the number “1” on the top row of keys) puts a grave accent over the next vowel typed. The tilda (“~”) is the same key shifted, so holding down both the CTRL and SHIFT keys while pressing that key, and then typing “n” or “N” will produce с or С.

How do I type é on my keyboard?

Unlike iOS, Android doesn't come preloaded with support for accented characters. However, there's an app for that—several of them, actually. Simply head to your app store and search for a “smart keyboard.” Choose whichever you like best, install it, then type accents, acute or otherwise, whenever you need.

How do I type special characters with accents in Windows 10?

To access it on your Windows 10 system: Type “character” in your search field and then select the Character Map app. You'll get a pop-up map showing a bunch of special characters for a specific font. You can change the font by clicking on the drop-down font menu at the top.


2 Answers

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));
like image 144
Emil Vikström Avatar answered Oct 17 '22 13:10

Emil Vikström


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.

like image 7
kaha Avatar answered Oct 17 '22 13:10

kaha