I need to search a string for any occurances of another string in PHP. I have some code that I’ve been playing with, but it doesn’t seem to be working.
Here is my code:
while (list($key, $val) = each($keyword)) {
$pos = strpos($location, $val);
if($pos == false) {
echo "allow";
exit;
} else {
echo "deny";
exit;
}
}
I have tried some of the options below, but it still does not find the match. Here is what I’m searching:
I need to find:*
blah
In:
http://blah.com
Nothing seems to find it. The code works in regular sentences:
Today, the weather was very nice.
It will find any word from the sentence, but when it is all together (in a URL) it can’t seem to find it.
When checking for boolean
FALSEin php, you need to use the===operator. Otherwise, when a string match is found at the0index position of a string, yourifcondition will incorrectly evaluate totrue. This is mentioned explicitly in a big red box in the php docs forstrpos().Also, based on a comment you left under another answer, it appears as though you need to remove the
exitstatement from the block that allows access.Putting it all together, I imagine your code should look like this:
Update:
With the new information you’ve provided, I’ve rewritten the logic: