tags:

views:

104

answers:

3

I want to convert special characters like ñ, Ñ to htmlentities using php.

I tried using htmlentities, but instead of returning "&ntilde" for its value it returns "ñ" as its value.

A: 

You need to specify the character set you use as the third parameter to htmlentities(). The default character set is iso-8859-1. If you use UTF-8 for your data, you need to say so:

$result = htmlentities($string, ENT_QUOTES, "UTF-8");
Pekka
+1  A: 

Make sure that your page charset is set to utf-8

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Sarfraz
A: 

You have to specify the charset because the default is ASCII (http://php.net/manual/en/function.htmlentities.php):

htmlentities($stringToConvert, ENT_COMPAT, 'UTF-8')
Luca Bernardi
sorry typo: not ASCI but ISO-8859-1
Luca Bernardi