Edit report at https://bugs.php.net/bug.php?id=61354&edit=1
ID: 61354
Comment by: tokul at users dot sourceforge dot net
Reported by: hufeng1987 at gmail dot com
Summary: htmlentities and htmlspecialchars doesn't respect
the default_charset
Status: Not a bug
Type: Bug
Package: Strings related
Operating System: Linux/Windows/
PHP Version: 5.4.0
Block user comment: N
Private report: N
New Comment:
Two small comments.
Could you write your Chinese symbols in hex notation. That way they are more
friendly for pages written in other charset?
Your test code is
-----
<?php
$string = "<pre><p>\xce\xd2\xca\xc7\xb2\xe2\xca\xd4</p></pre>";
echo var_dump(htmlspecialchars($string));
echo var_dump(htmlspecialchars($string, NULL, 'GB2312'));
-----
Expected result - both var_dumps should be the same.
> htmlspecialchars should using charset defined by php.ini default_charset.
htmlspecialchars() should not use charset defined in PHP configuration. It
should use iso-8859-1 for backwards compatibility reasons.
Previous Comments:
------------------------------------------------------------------------
[2012-03-12 06:12:58] hufeng1987 at gmail dot com
When your project using GB2312 as default charset encoding, when you upgrade
to php 5.4, you will find htmlspecialchars will not working as usual.
if you want them working correctly, you should replace following code with new:
old code:
htmlspecialchars($string);
new code:
htmlspecialchars($string, NULL, 'GB2312');
recoding the full project is a huge work.
especially when the project is old.
------------------------------------------------------------------------
[2012-03-12 06:05:54] hufeng1987 at gmail dot com
may be you are right , php 5.4 should have utf-8 as the default encoding.
but , as production enviroment, this will cause more accident.
why not php wisely handle default_charset ? that will free us from recoding.
------------------------------------------------------------------------
[2012-03-12 06:04:35] [email protected]
What do you mean it is impossible to rewrite old code? In previous versions
htmlspecialchars() didn't respect the default_charset ini setting either. It
only
looks at that setting if you pass an empty string as the encoding. The change
in
PHP 5.4 was simply to switch from ISO-8859-1 to UTF8 when you do not specify a
charset.
------------------------------------------------------------------------
[2012-03-12 05:56:17] hufeng1987 at gmail dot com
if this was not a bug, why this change blocked our old project?
in previous PHP under php 5.4 , we could using htmlspecialchars as simple:
htmlspecialchars($string);
and this call should not broken the string.
but now, under php 5.4, the default encoding change to utf-8. which may broken
old codes.
it is impossible to rewrite old code ,add charset encoding specified.
------------------------------------------------------------------------
[2012-03-12 05:47:19] [email protected]
There is some confusion around this point. The default_charset in your php.ini
file is meant to be the output encoding. What you specify here is what ends up
in the HTTP Content-type response header. You should be able to change that
without messing up your internal runtime encoding which is why setting that
does
not automatically change the internal encoding used by
htmlspecialchars/htmlentities. You can force it to look at it by setting the
3rd
arg (the encoding) arg of the htmlspecialchars() call to "" (and empty string).
This is documented on the http://php.net/htmlspecialchars page. But, like I
mentioned, you should be able to change your output encoding separately from
your internal runtime encoding, so we don't suggest doing this. The safest
approach is to explicitly set your encoding on your htmlspecialchars() calls.
There times when you get data from sources that have different encodings so two
htmlspecialchars() calls in the same app may need to use different encodings.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=61354
--
Edit this bug report at https://bugs.php.net/bug.php?id=61354&edit=1