简体   繁体   English

如何使用PHP删除文本字段上的特殊字符和空格

[英]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.

相关问题 我试图删除特殊字符,但保留空格和_不工作PHP - I am trying to remove special characters but keep spaces and _ not working PHP 在 php 如何删除所有特殊字符、大写字母、数字和空格 - in php how to remove all special characters, uppercase letters, numbers and spaces 如何在PHP中删除特殊字符 - How to remove the special characters in PHP 如何在PHP中删除特殊字符 - How to remove special characters in php PHP函数/脚本,用于合并数据库中的2列并删除其特殊字符和空格 - php function/script for combining 2 columns in a database and remove their special characters and spaces 如何删除所有特殊字符和空格,但逗号(,),冒号(:),双引号(“)和字符串的花括号除外 - How to remove all special characters and spaces except Comma(,), Colon(:),Double quote(") and curly braces of a string PHP 如何删除PHP字符串中的特殊字符? - How can I remove special characters in a PHP string? 如何在PHP中编写正则表达式以删除特殊字符? - How do I write a regex in PHP to remove special characters? 如何使用php和regex从文本中删除特殊字符 - How to remove special characters from text using php and regex 如何将特殊字符从文本字段(&#39;+ &lt;&gt; $“)保存到数据库中,然后使用PHP检索它们? - How do I save special characters from a textfield ('+<>$") into a database, then retrieve them with PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM