【发布时间】:2019-08-31 13:49:03
【问题描述】:
为什么 filter_var() 的 FILTER_SANITIZE_STRING 过滤器将单引号编码为',将双引号编码为",而 htmlentities() 将单引号编码为',而将双引号编码为"?
代码示例:
<?php
$string = "Well that's \"different.\"";
echo "filter_var: ".filter_var($string, FILTER_SANITIZE_STRING)."\n";
echo "htmlentities: ".htmlentities($string, ENT_QUOTES)."\n";
echo "htmlspecialchars: ".htmlspecialchars($string, ENT_QUOTES)."\n";
输出:
filter_var: Well that's "different."
htmlentities: Well that's "different."
htmlspecialchars: Well that's "different."
【问题讨论】:
-
你当然可以向 Rasmus Lerdorf 提出同样的问题。
-
最后它们是等价的,所以……
-
@deceze 这是基于意见的吗?这个不知道怎么回答。
-
@deceze True,除非您要对以两种不同方式编码的字符串进行字符串比较。
-
@mick 这可能是有原因的。我猜
filter_*的实现非常通用,将不在某个白名单中的任何内容替换为其字符代码;而html*更擅长 HTML 并且知道所有别名。
标签: php encoding html-entities sanitization filter-var