php - 如何过滤任何特殊字符

标签 php

如果我以这样的形式输入文本

<form action="add.php" method="post">
<input type="text" name="txt" id="txt">
<input type="submit" name="submit" value="Submit">
</form>

add.php代码如下

$sql= "insert into mytable set myname='$txt'";
executeupdate($sql);

我的问题是如何过滤来自任何特殊字符的任何文本输入,我只想要它 Aa-Zz-1234567890(字母和数字)。

这是我的尝试,但我不确定它是否真的会过滤所有特殊字符或可以传递任何

我在 add.php 中添加了以下代码

$cleared = strip_tags($txt);
$sql= "insert into mytable set myname='$cleared'";
executeupdate($sql);  // this should clear the name before insert

还有其他建议或更好的方法吗?

最佳答案

$txt = preg_replace('/[^a-zA-Z0-9]/', '', $txt);

此外,A-Z、0-9 范围之外的字符没有任何“特殊”之处。

关于php - 如何过滤任何特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7057307/

相关文章:

php - 如何从 MySQL 表中选择具有最大值的行

PHP 提示动态返回类型(基于 $class 参数)

php - 我的购物车产品添加和减去无法正常工作

php - JQUERY-PHP-MYSQL - 从 Mysql 到现有 DIV 列表的数据没有响应点击事件

php - 如何将 webP 图片格式转换为普通格式? php

php - 多个项目 : Dynamic Item Name and Number in PayPal Form with PHP

php - 我如何在 Laravel 中使用 BETWEEN 和 AND

php - OpenCart 如何将商店详细信息获取到 Controller

php - 反色情图片上传

php - preg_match 获取文本