Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3362360
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:15:53+00:00 2026-05-18T03:15:53+00:00

I need to search a string for any occurances of another string in PHP.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T03:15:54+00:00Added an answer on May 18, 2026 at 3:15 am

    When checking for boolean FALSE in php, you need to use the === operator. Otherwise, when a string match is found at the 0 index position of a string, your if condition will incorrectly evaluate to true. This is mentioned explicitly in a big red box in the php docs for strpos().

    Also, based on a comment you left under another answer, it appears as though you need to remove the exit statement from the block that allows access.

    Putting it all together, I imagine your code should look like this:

    while (list($key, $val) = each($keyword)) {
      $pos = strpos($location, $val);
      if($pos === false) { // use === instead of ==
        echo "allow";
      } else {
        echo "deny";
        exit;
      }
    }
    

    Update:

    With the new information you’ve provided, I’ve rewritten the logic:

    function isAllowed($location) {
        $keywords = array('blah', 'another-restricted-word', 'yet-another-restricted-word');
        foreach($keywords as $keyword) {
            if (strpos($location, $keyword) !== FALSE) {
                return false;
            }
        }
        return true;
    }
    
    $location = 'http://blah.com/';
    echo isAllowed($location) ? 'allow' : 'deny';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a search method that takes in a user-entered string, splits it at
I have a string in a django/python app and need to search it for
I have the need to search for an item that exists in one of
I'm writing some code and need to search for some kinds of symbols in
I need to search a string and replace all occurrences of %FirstName% and %PolicyAmount%
I need to search a string in the string array. I dont want to
I need to recursively search directories and replace a string (say http://development:port/URI ) with
I have a multidimensional array. I need to search it for a specific range
Problem I have timestamped data, which I need to search based on the timestamp
I need a binary search algorithm that is compatible with the C++ STL containers,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.