2

服务器正在运行PHP 5.2.17,我正在尝试使用三个参数运行 get_html_translation_table() 。这是我调用该函数的方式:

$text = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "UTF-8");

我收到一条警告消息说

get_html_translation_table 最多需要 2 个参数,3 个给定(文件名和行号)。

根据PHP Documentation , PHP 5.3.4 之后支持第三个参数,但添加第三个参数是我能想到的对以“UTF-8”返回的数组进行编码的唯一方法。(尽管有丑陋的警告信息,它仍然有效。)

我需要 get_html_translation_table() 来创建一个对所有 html 特殊字符和空格进行编码的函数,如果没有第三个参数,以下函数将无法工作。

/**
 * Trying to encoding all html special characters, including nl2br()
 * @param string  $original
 * @return string
 */
function ecode_html_sp_chars($original) {
    $table = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "UTF-8");
    $table[' '] = ' ';
    $encoded = strtr($original, $table);
    return nl2br($encoded);
}
4

1 回答 1

1

两个选项:更改您的 php 版本或使用 htmlentities 函数。在 htmlentities 中,编码参数是在 4.1 中添加的。

例子:

function ecode_html_sp_chars($original) {
    $encoded = htmlentities($original, ENT_QUOTES, "UTF-8");
    $encoded = str_replace(' ', ' ', $encoded);
    return nl2br($encoded);
}
于 2012-11-16T07:20:42.100 回答