2011-02-24 94 views
0

嗨,大家好我不知道我怎么能建立电子正则表达式,上面写着:的preg_match多个表达式

“此字符串可能包含1-25字母不是这些具体的话:”根“‘宾’,”下载”, “关机”

所以我想:

$dang_words="/(root)|(bin)|(shutdown)|(download)/"; 
$reg_exp="/^[a-z]{1,25}$/"; 

if(preg_match($reg_exp,$field) || !preg_match($dang_words,$field)) 
{ 
echo "your input it's okkk!"; 
} 
else 
echo "this is a bad word!!"; 

但它不工作

为什么

感谢

卢卡

+0

你的意思是说,字符串不能是*坏字之一,或者不能*包含*坏字之一?那么,'rubinia'会被允许与否? – 2011-02-24 18:05:48

+0

只需更改||以&&在你的if。 – amitchd 2011-02-24 18:07:37

+0

我只是想让字母表字母,但不会不会有任何危险的词! – luca 2011-02-24 18:52:54

回答

4
$dangerous_words="/(root)|(bin)|(shutdown)|(download)/"; 
$reg_exp="/^[a-z]{1,25}$/"; 

if(preg_match($reg_exp,strtolower(trim($field))) && !preg_match($dangerous_words,strtolower(trim($field)))) 
{ 
echo "your input it's okkk!"; 
} 
else 
echo "this is a bad word!!"; 

你有你的逻辑运算符搞砸了..刚刚从||更改到& &。

+1

感谢什么羞辱! = P – luca 2011-02-24 18:54:37

0

我觉得你的问题在于所有的()。在我刚刚创建的一些旧代码中,我使用了这个:

$stopInjectionVerbs = "/(alter|begin|cast|convert|create|cursor|declare|delete|drop|end|exec|fetch|insert|kill|open|select|sys|table|update)/"; 
$errors = array(); 

if (preg_match($stopInjectionVerbs, $select)) { 
    $errors[] = "Can not use SQL injection Strings"; 
} 

这一切工作正常。如果没有括号,请在每个单词旁边去。

+0

我刚刚测试('根')没有所有的括号..它仍然说“你的输入没关系! – luca 2011-02-24 18:09:05

4

关闭...试试这个:

/^(?!.*(root|bin|shutdown|download))[a-z]{1,25}$/ 

它采用了forward assertion

所以,就变成:

if (preg_match('/^(?!.*(root|bin|shutdown|download))[a-z]{1,25}$/', $content)) { 
    echo "Your input is ok"; 
} else { 
    echo "there is a bad word/invalid content"; 
} 
+1

小心解释'-1'?它在一个正则表达式中工作,而不是两个...? – ircmaxell 2011-02-24 18:21:16

+0

我想这取决于他的意图的解释。你的一个正则表达式不同于他的两个正则表达式的组合。如果该字符串与其中一个不良词完全匹配,则只会将该字符串标记为不良。如果字符串包含字符串中任何位置的错误字词,他的方法会将该字符串标记为不良。 – 2011-02-24 19:21:24

+0

@brett:谢谢,我没有意识到这一点。但现在知道,我可以解决这个问题;-) – ircmaxell 2011-02-24 19:31:03