繁体   English   中英

更改 PHP 函数的默认字符集,如“htmlspecialchars”

[英]change default character set of PHP functions like “htmlspecialchars”

我使用的是 PHP 5.2.6,我的应用程序的字符集是 UTF-8。

现在,我应该如何更改 PHP 的默认字符集? 不是指定输出的 MIME 时间和字符集的那个。

但是对于所有 PHP 函数,如 htmlspecialchars、htmlentities 等,这将发生变化。

我知道,这些函数中有一个参数接受输入字符串的字符集。 但我不想为我使用的所有功能指定。 如果我忘记了某处,那将是一团糟。

我也知道,我可以包装这些函数并创建自己的包装器,例如:

function myHtmlize($str)
{
  return htmlspecialchars($str, ENT_COMPAT, 'UTF-8');
}

我也不喜欢这个解决方案。

我真的很想告诉 PHP,默认情况下将 'UTF-8' 作为字符集。 不是“iso-8859-1”。

是否有可能?

像这个 ? http://us2.php.net/manual/en/function.setlocale.php

* LC_ALL for all of the below
* LC_COLLATE for string comparison, see strcoll()
* LC_CTYPE for character classification and conversion, for example strtoupper()
* LC_MONETARY for localeconv()
* LC_NUMERIC for decimal separator (See also localeconv())
* LC_TIME for date and time formatting with strftime()
* LC_MESSAGES for system responses (available if PHP was compiled with libintl)

which is used to find the "right" charset based on有一个 C 函数用于根据以下条件查找“正确”字符集

以该顺序并取决于某些扩展是否内置。
this is called with charset_hint=NULL and the first this function does is: “问题”是,当您调用_charset 将使用 charset_hint=NULL 调用,并且此函数执行的第一个操作是:

/* Guarantee default behaviour for backwards compatibility */
if (charset_hint == NULL)
    return cs_8859_1;

您必须至少调用 htmlentities('xyz', ENT_QUOTES, '' )

我不完全确定,但我认为mbstring.func_overload适用于htmlentities.

htmlspecialchars是字符htmlspecialchars性顺便说一句。 (至少只要字符集支持 utf-8 支持的 ascii 子集)。

来自 php.net:

5.4.0 编码参数的默认值已更改为 UTF-8。

在现代,您只需将 PHP 版本更新到 5.4 或更高版本,并将 php.ini 中的 default_charset 指令设置为 UTF-8(这已经是默认值)。 您也可以像这样以编程方式执行此操作:

ini_set('default_charset', 'UTF-8');

资料来源:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM