Check if a PHP String Contains Multiple Words with Example

Summary

In this post, you will learn a particular string contains multiple words or not. Here we will guide you through multiple methods to achieve this using multiple PHP built-in functions like strops, str_contains, str_ends_with, and str_starts_with

Introduction

String manipulation is a core part of programming, and PHP offers multiple functions to make it easier. Let us see multiple examples to check if a string contains multiple words using different techniques.

Using strpos() function

<?php

$haystack = "Welcome to di tutorials.";
$keywords = ["di", "Welcome", "tutorials"];

$foundKeywords = [];

foreach ($keywords as $keyword) {

    if (strpos($haystack, $keyword) !== false) {

        $foundKeywords[] = $keyword;

    }

}

if (!empty($foundKeywords)) {

    $foundKeywordsStr = implode(", ", $foundKeywords);

    echo "The words '$foundKeywordsStr' were found in the string.";

} else {

echo "None of the keywords match in the given string.";

}
?>
The words 'di, welcome, tutorials' were found in the string.

Using str_contains() to Check for Multiple Words:

The str_contains function is introduced in PHP 8 and is ideal for checking if a string contains a specific substring, such as multiple words.

<?php
$sentence = "The quick brown fox jumps.";

$keywords = ["quick", "jumps"];

foreach ($keywords as $keyword) {

    if (str_contains($sentence, $keyword)) {

        echo "The sentence contains '$keyword'.<br>";

    } else {

        echo "The sentence does not contain '$keyword'.<br>";

    }

}
?>

Result

The sentence contains 'quick'.
The sentence contains 'jumps'.

Using str_ends_with() to Check for Ending Words:

If you’re interested in verifying whether a string ends with specific words, str_ends_with comes in handy.

<?php
$phrase = "Stay hungry, stay foolish.";

$endings = ["foolish.", "dreams."];

foreach ($endings as $ending) {

    if (str_ends_with($phrase, $ending)) {

        echo "The phrase ends with '$ending'.<br>";

    } else {

        echo "The phrase does not end with '$ending'.<br>";

    }

}
?>

Result

The phrase ends with 'foolish.'.
The phrase does not end with 'dreams.'.

Using str_starts_with() to Check for Starting Words:

To determine if a string starts with certain words, you can use str_starts_with.

<?php

$text = "Coding is fun and rewarding.";
$beginnings = ["Coding", "Programming"];

foreach ($beginnings as $beginning) {
    if (str_starts_with($text, $beginning)) {
        echo "The text starts with '$beginning'.<br>";
    } else {
        echo "The text does not start with '$beginning'.<br>";
    }
}

?>

Result

The text starts with 'Coding'.
The text does not start with 'Programming'.

FAQs:

Q. How do I check if a string contains multiple words?

Ans. You can use the str_contains function to check if a string contains specific words.

Q. How to check if a string contains some words in PHP?

Ans. By looping through an array of target words and using the str_contains function.

Q. How to check if a string contains any letters in PHP?

Ans. To check if a string contains any character, you can use regex or iterate through each character with loop.

How to match a string in PHP?

Ans. For more complex matching, you can use regular expressions or functions like strpos and preg_match.

Conclusions:

This article taught you how to check for various situations to see if a PHP string contains multiple words. In PHP web applications, the str_contains, str_ends_with, and str_starts_with functions offer the best approach to achieve this.

%d bloggers like this: