在php搜索中不允许*

在php搜索中不允许*,php,Php,我想禁止输入(例如)p*的用户对数据库进行搜索 我不知道如何将它添加到我已有的代码中。我猜在$trimmed=str\u replace(“\”,“'”,trim($search));是答案,用数组替换“\”,但我似乎找不到正确的方法。如果我只是用*替换\,我就可以让它工作,但是我失去了“\”字符的修剪:这有关系吗 // Retrieve query variable and pass through regular expression. // Test for unacceptable c

我想禁止输入(例如)p*的用户对数据库进行搜索

我不知道如何将它添加到我已有的代码中。我猜在
$trimmed=str\u replace(“\”,“'”,trim($search));
是答案,用
数组替换
“\”
,但我似乎找不到正确的方法。如果我只是用
*
替换
\
,我就可以让它工作,但是我失去了
“\”
字符的修剪:这有关系吗

// Retrieve query variable and pass through regular expression.
// Test for unacceptable characters such as quotes, percent signs, etc.
// Trim out whitespace.  If ereg expression not passed, produce warning.
$search = @$_GET['q'];
// check if wrapped in quotes
if ( preg_match( '/^(["\']).*\1$/m', $search ) === 1 ) {
   $boolean = FALSE;
}

if ( escape_data($search) ) {
   //trim whitespace and additional disallowed characters from the stored variable
   $trimmed = str_replace("\"","'",trim($search));
   $trimmed = stripslashes(str_ireplace("'","", $trimmed));
   $prehighlight = stripslashes($trimmed);
   $prehighlight = str_ireplace("\"", "", $prehighlight);
   $append = stripslashes(urlencode($trimmed));
} else {
   $trimmed = "";
   $testquery = FALSE;
}

$display = stripslashes($trimmed);

您已经说过了,只需使用数组作为str_repace的参数:

第一个数组中的每个元素都将替换为第二个数组中的元素


对于将来的验证和卫生,您可能还需要阅读有关此函数的内容:


使用
$search=mysql\u real\u escape\u string($search)
它将从
$search
中删除可能影响查询的所有字符。

首先,
转义\u data
在做什么?啊!我不知道!这是我买的剧本,因为我自己的剧本一直在崩溃!我希望这个脚本能保护我们免受SQL注入攻击,而这正是我认为escape_data所做的。我错了吗?如果是的话,救命!明亮的谢谢我一定是有一个盲点,因为我无法找到一个阵列所需要的替代品。我刚刚为我想要替换的项定义了数组是的,我希望str_replace会接受一个项作为第二个参数,并用它替换每个项,但它没有:)谢谢你的建议欢迎
$trimmed = str_replace( array("\"", "*"), array("'", ""), trim($search) );