[英]How to I remove special characters and spaces on a textfield using 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.
我如何在PHP中完成此任务。
This really depends, I assume you are working with $_POST[] data and wish to sanitize those inputs? 这实际上取决于,我假设您正在使用$ _POST []数据并希望清理这些输入? If so I would definitely do something like:
如果是这样,我肯定会这样做:
$var = preg_replace("/[^A-Za-z0-9]/", "", $var);
That will strip out everything other than alpha/num, you can adjust the regex to include other characters if you wish. 除了alpha / num之外,它将删除除alpha / num之外的所有内容,如果您愿意,可以调整正则表达式以包含其他字符。 Some great examples of commonly used regular expressions can be found at: The RegEx Library
可以在以下位置找到常用正则表达式的一些很好的示例: RegEx库
If this isn't quite what you are looking for or have other questions let us know. 如果这不是您正在寻找的或有其他问题,请告诉我们。
Use the following regex during processing of the data: 在处理数据期间使用以下正则表达式:
$data = preg_replace('/[^A-Za-z0-9]/', "", $data);
This will remove all non-alphanumeric characters from the data. 这将从数据中删除所有非字母数字字符。
$specialChars = array(" ", "\r", "\n");
$replaceChars = array("", "", "");
$str = str_replace($specialChars, $replaceChars, $str);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.