如何使用PHP删除文本字段上的特殊字符和空格
我需要删除我正在构建的表单的文本字段上的所有特殊字符和空格.如何在PHP中完成此操作.
I need to remove all special characters and spaces on a textfield for a form I'm building. How do I accomplish this in PHP.
这真的取决于我是否假设您正在使用$ _POST []数据并希望清理这些输入?如果是这样,我肯定会做类似的事情:
This really depends, I assume you are working with $_POST[] data and wish to sanitize those inputs? If so I would definitely do something like:
$var = preg_replace("/[^A-Za-z0-9]/", "", $var);
这将去除除alpha/num之外的所有内容,您可以根据需要调整正则表达式以包含其他字符.可以在以下位置找到一些常用正则表达式的出色示例: RegEx库
That will strip out everything other than alpha/num, you can adjust the regex to include other characters if you wish. Some great examples of commonly used regular expressions can be found at: The RegEx Library
如果这不是您要查找的内容,或者有其他问题,请告诉我们.
If this isn't quite what you are looking for or have other questions let us know.