如何使用PHP删除文本字段上的特殊字符和空格 [英] How to I remove special characters and spaces on a textfield using PHP

查看:263
本文介绍了如何使用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.

这篇关于如何使用PHP删除文本字段上的特殊字符和空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆