let htmlspecialchars use UTF-8 as default charset?

2019-05-30 16:42发布

Is there a way to tell PHP to use UTF-8 as default for functions like htmlspecialchars ?

I have already setted this:

ini_set('mbstring.internal_encoding','UTF-8');
ini_set('mbstring.func_overload',7);

If not, please can you post a list of all functions where I need to specify the charset?

(I need this because I am re-factorizing all my framework to get working with UTF-8)

2条回答
ら.Afraid
2楼-- · 2019-05-30 17:46

Just use htmlspecialchars() instead of htmlentities(). Because it doesn't touch the non-ASCII characters, it doesn't matter whether you use 'utf8' charset or the default 'latin1'(*), the results are the same. As a bonus your output is smaller. (Though it does mean you have to ensure you're actually serving your page with the correct encoding.)

(*: there are a few East Asian multibyte charsets which can differ in their use of ASCII code points, so if you're using those you would still need to pass a $charset argument to htmlspecialchars(). But certainly no such problem for UTF-8.)

查看更多
干净又极端
3楼-- · 2019-05-30 17:46

Is there a way to tell PHP to use UTF-8 as default for functions like htmlspecialchars ?

Nope, not as far as I know. mbstring.internal_encoding will define a default encoding for the mb_* family of functions only.

If not, please can you post a list of all functions where I need to specify the charset?

I'm not sure whether such a list exists - if in doubt, just walk through the manual and look out for any charset parameters.

查看更多
登录 后发表回答