如果我以这样的形式输入文本
<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/