note 94572 added to function.htmlentities
| From: | insid0r at yahoo dot com | Date: | Thu, 12 Nov 2009 16:38:24 +0000 |
| Subject: | note 94572 added to function.htmlentities | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162811@lists.php.net to get a copy of this message | ||
@ 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
?>
----
Server IP: 208.69.120.35
Probable Submitter: 143.44.175.254
----
Manual Page -- http://www.php.net/manual/en/function.htmlentities.php
Edit -- https://master.php.net/note/edit/94572
Del: integrated -- https://master.php.net/note/delete/94572/integrated
Del: useless -- https://master.php.net/note/delete/94572/useless
Del: bad code -- https://master.php.net/note/delete/94572/bad+code
Del: spam -- https://master.php.net/note/delete/94572/spam
Del: non-english -- https://master.php.net/note/delete/94572/non-english
Del: in docs -- https://master.php.net/note/delete/94572/in+docs
Del: other reasons-- https://master.php.net/note/delete/94572
Reject -- https://master.php.net/note/reject/94572
Search -- https://master.php.net/manage/user-notes.php