-5

您好我正在尝试解析字符串字符以防止 sql 注入和其他黑客攻击。我不想使用 mysql_real_escape_string 或其他过滤器。我只想使用正则表达式并使用字符 AZ 0-9 !@#$-_

我的意思是我可以使用正则表达式,例如:

$newStr = preg_replace('/[^a-z0-9]/i', '_', $str);

但我只想安全,我不太擅长正则表达式。再次感谢大家,你们真的很棒。

4

1 回答 1

0

这:

$newStr = preg_replace('/[^a-z0-9]/i', '_', $str);

应该:

$newStr = preg_replace('/[^a-zA-Z0-9!@#$-]/', '_', $str);

下面的代码应该去掉:

'"/\;?"

<?php
        $newStr = preg_replace('/[^a-zA-Z0-9!@#$-]/', '_', "test\'\"\/\\\;\?");
        echo $newStr;
?>

产生:

test__________%
于 2013-03-22T22:16:30.060 回答