let htmlspecialchars use UTF-8 as default charset?

笑着哭i 提交于 2020-01-14 03:36:07

问题


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)


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/6181299/let-htmlspecialchars-use-utf-8-as-default-charset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!