text
text

Reputation: 363

special characters to html entity using php

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.

Upvotes: 0

Views: 1629

Answers (3)

Luca Bernardi
Luca Bernardi

Reputation: 4199

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')

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382909

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

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

Upvotes: 1

Pekka
Pekka

Reputation: 449823

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");

Upvotes: 0

Related Questions