String contains multiple words

There are several ways to determine if a given string contains a specific word in PHP. It depends on the program context we are solving.

As we are here to check that a PHP string contains multiple words, therefore, assuming that you are already aware of a single word search within the string. Therefore first we will see how to check if a string contains any word in short.

One of the best ways to check single word using PHP function ​​strpos

Example:

$searchText = "QuizCure";

$givenString = "Programming articles listed on QuizCure belongs to java, python, PHP and more";
 

if(strpos($givenString, $searchText) !== false){
    echo "QuizCure text Found!";
} else{
    echo "QuizCure text Not Found!";
}

Result: QuizCure text Found!

Explanation:

  • strpos is used to check the position of the first existence of a search word
  • strpos is case-sensitive. Therefore it will match the same case letters only.
  • In our example above we have searchText as QuizCure and the given string also contains the same set of upper and lowercase combinations of word QuizCure.
  • strpos($givenString, $searchText) return false if it doesn’t find the searchText position in the given string. Therefore I used criteria strpos($givenString, $searchText) !== false to test.

Now let’s explore how can we check string containing more than one word in a given string in the following ways

1. Check if the string contains multiple words using the preg_match function

preg_match function is used to perform searches for regular expression patterns on passing strings.

Syntax:

preg_match($pattern, $givenString, $matchesFound, $flags, $offset)

  • $pattern: Required param. Pattern to check in a given string
  • $givenString: Required Param. givenString is text in which search for patterns needs to be performed.
  • $matchesFound: Its optional parameter. It will be populated with search results if this param is provided.
  • $flags: Optional param.
  • $offset: Optional param.
  • Return:
    1 => If pattern matched
    0=> If pattern doesn’t meet
    False => Once failed

Our objective here is to find regular expressions to match with multiple words on a given text. Let's explore in following example code

$pattern = "(Quizcure|programming|java)"; 
$givenString = "Programming articles listed on Quizcure belongs to python, java, PHP and more";
var_dump(preg_match($pattern, $givenString));
  

Result: int(1)

Explanation:

  • We need to construct a regular expression for multi-words by separating using | symbol as (Quizcure|programming|java)
  • Therefore it returns 1 if any of the word patterns matched in the given string. In our case, all used words are part of a given string.

Case1: one of three words matched only

$pattern = "(Quizcure|abc|xyz)"; 
$givenString = "Programming articles listed on Quizcure belongs to python, java, PHP and more";
 
var_dump(preg_match($pattern, $givenString));

Result: int(1)

Case 2 : None matched

$pattern = "(demo|abc|xyz)"; 
$givenString = "Programming articles listed on Quizcure belongs to python, java,  PHP and more";
 
var_dump(preg_match($pattern, $givenString));

Result: int(0)

2. Check case-insensitive multiple matching using preg_match function

In the previous example, we learned about multiple word matching using regex. It was a case-sensitive operation.

Now we will see a case-insensitive multiple word matching demonstration.


$pattern = "/(quizcure|abc|java)/i"; 

$givenString = "Programming articles listed on QuizCure belongs to java, python, PHP and more";
 
var_dump(preg_match($pattern, $givenString));

Result: int(1)

Program Explanation:

  • i modifier in Regular expression is used here to ignore-case.
  • /(quizcure|abc|java)/i with i doesn’t care whether the input string contains the word Capital letter, lower or mixed letter.
  • output of preg_match($pattern, $givenString) will be 1 once any word matched from the string for a given pattern /(quizcure|abc|java)/i . In our example here it matched with word quizcure hence returning 1

Share String contains multiple words