开发者

preg_match multiple expressions

hi guys i was wondering how could i build e regExp that says:

"this string may contain 1-25 letters that are not these specific words:"root","bin","downl开发者_开发问答oad","shutdown"

So I thought:

$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!!";

But it's not working

why?

thanks

Luca


$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!!";

You have your logical operators messed up.. Just changed from || to &&.


Close... Try this:

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

It uses a forward assertion

So, it becomes:

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";
}


I think your issue lies with all the ( ). In some old code I created a while ago I used this:

$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";
}

This all works correctly. Have a go without the brackets around each individual word.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜