我想剥离 SQL 可执行语句的用户输入,但代码库的方式是我不想建立 mysqli_real_escape_string(connection, string) 需要的数据库连接。
什么是优雅的正则表达式/替换单线来清理字符串?
<?php
$naughty = array("\\", "{", "}", ")", "(", "[", "]", "\"", ";", ":", ">", "&");
$sanitized = str_replace($naughty, " ", $input_string);
?>
---更新----示例解决方案。感谢您的输入。
<?PHP
$testsql = "';\"\\ bork bork bork %A 0x33\n";
echo $testsql;
/* $translate = array("0x" => "[HEX]", ";" => "[SEMICOLON]", "'" => "[QUOTE]", "%" => "[PERCENT]","\"" => "[DOUBLEQUOTE]",
"\\" => "[BACKSLASH]" ); */
$translate = array("0x" => " ", ";" => " ", "'" => " ", "%" => " ","\"" => " ", "\\" => " ");
echo "<br>";
$testsql=strtr($testsql, $translate);
echo $testsql;
?>