我想从输入字符串中清除<
和>
字符,以使其"网络安全"并避免脚本注入恶作剧。
我知道 strip_tags() 可以用于此目的,但这会导致如下字符串:
We are looking at counts < 5000 for this test run
截断为:
We are looking at counts
.
我想将其转换为:
We are looking at counts 5000 for this test run
这并不完美,但字符串中的信息会以这种方式丢失。
我知道这可以通过例如正则表达式来实现,但这在任何方面都不安全吗?我的意思是,strip_tags() 是否对字符串做了一些刚刚删除<>
的正则表达式不会做的事情?
我现在不想使用 htmlentities(),因为这会混淆我们的前端代码。
如果您只想剥离<
并>
使用以下代码执行此操作:
$str = 'We are looking at counts < 5000 for this test run';
$convertedBracketStr = str_replace(array('<', '>'), null, $str);
其结果将是 :We are looking at counts 5000 for this test run
http://php.net/manual/en/function.htmlspecialchars.php
这会将<、>和许多其他字符转换为其 HTML 实体,以便它们在页面上正确显示。