note 94572 deleted from function.htmlentities by danbrown
| From: | danbrown@php.net | Date: | Thu, 12 Nov 2009 16:41:14 +0000 |
| Subject: | note 94572 deleted from function.htmlentities by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162812@lists.php.net to get a copy of this message | ||
Note Submitter: insid0r at yahoo dot com
----
@ montana (Jul-2009):
I think you're operating under a misunderstanding. It looks like you've saved your PHP
source code (or are in some other way giving your input) in UTF-8 encoding, without telling
htmlentities() that the input is in UTF-8.
By default, htmlentities() assumes input is encoded in ISO-8859-1 (single byte, fixed-width
character encoding). If you pass it "ñ" (ISO character 241), it returns ñ
just fine. If you pass it "ñ" (this is technically the 2-byte UTF-8 sequence
representing ñ) and do not specify UTF-8 as the encoding to htmlentities(), it will return
ñ, because those are the corresponding entities for those ISO characters.
Try specifying "UTF-8" as the input encoding, and it should work. If you are applying
htmlentities() to form input, many browsers will default to posting in UTF-8, even if the character
encoding of the site is ISO-8859-1. Other browsers will try to use the encoding of the page.
Specify
ACCEPT-CHARSET as UTF-8 in your FORM, and use utf8_encode() before processing to make sure
everything works uniformly (input sanitation is _always_ a good idea.)
Example proof of concept:
<?php
$ntilde = chr(241); //This = ñ in ISO-8859-1
$ntildeUTF = utf8_encode($ntilde); //This = ñ in ISO-8859-1, or ñ in UTF-8
echo htmlentities($ntilde); //returns "ñ"
echo htmlentities($ntilde, null, "UTF-8"); //returns "" (input is not a valid
UTF-8 sequence)
echo htmlentities($ntildeUTF); //returns "ñ" because that's
what the UTF-8 looks like in one-byte ASCII
echo htmlentities($ntildeUTF, null, "UTF-8"); //returns "ñ" as it
should
?>