PHP htmlentities trim 产生异常结果 - 删除数字 3
PHP htmlentities with trim producing unusual result - removes number 3
我觉得这可能是一个错误,但它也可能是 PHP 的 htmlentities 和 trim 结合使用的一个鲜为人知的怪癖。
此代码可以很好地处理发送到其中的任何数据:
// Number is normally accessed using $_Post[], hard coded for example
$rating = htmlentities(trim('3'));
但是,如果您将 ENT_QUOTES 参数添加到 htmlentities,您将得到意想不到的结果:
// Number is normally accessed using $_Post[], hard coded for example
$rating = htmlentities(trim('3',ENT_QUOTES)); // Returns ''
$rating = htmlentities(trim('3123',ENT_QUOTES)); // Returns 12
$rating = htmlentities(trim('test3123',ENT_QUOTES)); // Returns test312
有人知道是什么导致了这个错误吗?这似乎是 PHP 中的一个硬编码问题,所以我会解决这个问题,但我很好奇htmlentities 正在做什么导致了这种情况。
PS - 我正在使用 PHP 版本 5.5.11
您放置的括号不正确。在您的情况下,您将 ENT_QUOTES
作为 trim 的第二个参数传递。 ENT_QUOTES
常量设置为 3
因此 trim 删除 3
.
ENT_QUOTES
应该是 htmlentities 的第二个参数:
htmlentities(trim('3'), ENT_QUOTES);
我觉得这可能是一个错误,但它也可能是 PHP 的 htmlentities 和 trim 结合使用的一个鲜为人知的怪癖。
此代码可以很好地处理发送到其中的任何数据:
// Number is normally accessed using $_Post[], hard coded for example
$rating = htmlentities(trim('3'));
但是,如果您将 ENT_QUOTES 参数添加到 htmlentities,您将得到意想不到的结果:
// Number is normally accessed using $_Post[], hard coded for example
$rating = htmlentities(trim('3',ENT_QUOTES)); // Returns ''
$rating = htmlentities(trim('3123',ENT_QUOTES)); // Returns 12
$rating = htmlentities(trim('test3123',ENT_QUOTES)); // Returns test312
有人知道是什么导致了这个错误吗?这似乎是 PHP 中的一个硬编码问题,所以我会解决这个问题,但我很好奇htmlentities 正在做什么导致了这种情况。
PS - 我正在使用 PHP 版本 5.5.11
您放置的括号不正确。在您的情况下,您将 ENT_QUOTES
作为 trim 的第二个参数传递。 ENT_QUOTES
常量设置为 3
因此 trim 删除 3
.
ENT_QUOTES
应该是 htmlentities 的第二个参数:
htmlentities(trim('3'), ENT_QUOTES);