-1

Just after some help, I have an array with disallowed words in which I cant show here as they are obscene.

What I want to do is check if $_POST['urlprefix'] != one of the array elements, I am wondering what the easiest way to do this is. Obviously the below only stops one element of the array.

Thanks for any help you may provide

if($_POST['urlprefix'] != $arr[1]) {
  print("You've Passed");
} else {
  print("You Are Not Allowed To Use That URL Prefix");
}
1
  • 6
    Check out in_array. Commented Jul 3, 2014 at 14:28

4 Answers 4

1

The examples with in_array() are a quick way to achieve that. You can also use a case-insensitive in_array function by using preg_grep.

But, if ship is a bad word, do you want to allow shippy ? The following code will forbid any prefixes that contain the word, too.

$disallow = false;
foreach($arr as $bad_word) {
    if(stripos($_POST['urlprefix'], $bad_word) !== false) {
         $disallow = true;
         break;
    }
}

if($disallow) {
    die('Bad words in URL!');
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the in_array function :

if (!in_array($_POST['urlprefix'], $arr))
{
    print("You've Passed");
}
else
{
    print("You Are Not Allowed To Use That URL Prefix");
}

http://www.php.net/manual/fr/function.in-array.php

1 Comment

Thanks, I didnt know that even existed :)
0

You can use this way. in_array is slower than this method. See more about array_flip.

$words = array('damn','shit');
$flip = array_flip($words);
if(!isset($flip[$_POST['urlprefix']])){
    //valid url
}

1 Comment

You can't do array_flip($words))[$_POST['urlprefix']] in PHP versions less than 5.4.
0

You can use php in_array function. It checks if a value exists in an array

if(!in_array($_POST['urlprefix'], $arr)) {
      print("You've Passed");
    } else {
      print("You Are Not Allowed To Use That URL Prefix");
    }

2 Comments

Please avoid only-code answers, explain a bit your code. For example, here you can tell that he's looking for in_array function, and put a link to the documentation.
Ok sorry, i will do that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.