随着 FILTER_SANITIZE_STRING 的弃用,“使用 htmlspecialchars 代替”是一个不完整的注释。FILTER_SANITIZE_STRING 的功能是 htmlspcialchars 和 (大约) strip_tags 的组合。为了真正的兼容性,可能需要一个 polyfil
<?php
function filter_string_polyfill(string $string): string
{
$str = preg_replace('/\x00|<[^>]*>?/', '', $string);
return str_replace(["'", '"'], [''', '"'], $str);
}
$string = "Some \"' <bizzare> string & to Sanitize < !$@%";
echo filter_var($string,FILTER_SANITIZE_STRING).PHP_EOL;
//Some "' string & to Sanitize
echo htmlspecialchars($string).PHP_EOL;
//Some "' <bizzare> string & to Sanitize < !$@%
echo strip_tags($string).PHP_EOL;
//Some "' string & to Sanitize < !$@%
echo htmlspecialchars(strip_tags($string,ENT_QUOTES)).PHP_EOL;
//Some "' string & to Sanitize < !$@%
echo filter_string_polyfill($string).PHP_EOL;
//Some "' string & to Sanitize