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 5960343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:48:49+00:00 2026-05-22T18:48:49+00:00

Must be simple but cant find my answer. How to test whether one of

  • 0

Must be simple but cant find my answer.
How to test whether one of the values in the array is contained in the string?
Output should be true or false.

$array = Array( 
   0 => 'word1',
   1 => 'word2',
   2 => 'New York'
   3 => 'New car' 
);

$string = "Its a sunny day in New York";

Trying to clarify. In this case array[3] should not be a match. Only array[2] should be.

  • 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-22T18:48:50+00:00Added an answer on May 22, 2026 at 6:48 pm

    Update:

    A word boundary independent solution would be to add spaces around the input string and the search words:

    $str = ' ' . $str . ' ';
    
    
    function quote($a) {
        return ' ' . preg_quote($a, '/') . ' ';
    }
    
    $word_pattern = '/' . implode('|', array_map('quote', $array)) . '/';
    
    if(preg_match($word_pattern, $str) > 0) {
    
    }
    

    or by looping over the terms:

    foreach($array as $term) {
        if (strpos($str, ' '. $term . ' ') !== false) {
            // word contained
        }
    }
    

    Both can be put in a function to simplify the use, e.g.

    function contains($needle, $haystack) {
        $haystack = ' ' . $haystack . ' ';
        foreach($needle as $term) {
           if(strpos($haystack, ' ' . $term . ' ') !== false) {
               return true;
           }
        }
        return false;
    }
    

    Have a look at a DEMO


    Old answer:

    You could use regular expressions:

    function quote($a) {
        return preg_quote($a, '/');
    }
    
    $word_pattern = implode('|', array_map('quote', $array));
    
    if(preg_match('/\b' . $word_pattern  . '\b/', $str) > 0) {
    
    }
    

    The important part are the boundary characters \b here. You will only get a match if the value you search for is a (sequence of) word(s) in the string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I must be missing something very simple, but can't find the answer to this.
This must be simple, but I can't seem to find the answer online nor
There must be a very simple answer, but I can't find it anywhere. I
This must be a dead simple answer but one I just can't seem to
This probably has a simple answer, but I must not have had enough coffee
This question might sound simple but I can't find the answer i'm looking for.
this might be fairly simple but I cant find my way around... I have
I think this must be simple but I can't get it right... I have
This must be simple, but I can't seem to figure it out. I am
this must be such a simple problem but can someone tell me why this

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.