我想清理用户的字符串输入。我想,以防止特殊字符和代码符号,如<
,>
,&
等,但允许space
和single quotes (')
。到目前为止我所做的是:
$query = preg_replace('/[^A-Za-z0-9\-]/', '', htmlentities($request->search_name, ENT_COMPAT));
当我使用上面的代码测试它时,它还会删除我需要的空格。任何帮助都受到高度赞赏。
编辑:
如果我输入
mark O'neal"<>!
我上面的代码打印错误:
markOnealquotltgt
它应该输出:
mark O'nealquotltgt
您只需要将空格和'
字符添加到您的字符集中:
$search_name = "mark O'neal\"<>!";
echo preg_replace("/[^A-Za-z0-9\- ']/", '', htmlentities($search_name, ENT_COMPAT));
输出:
mark O'nealquotltgt
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句