How to Check If a Word Is Contained in Another String Using PHP

How do I check if a string contains a specific word?

Now with PHP 8 you can do this using str_contains:

if (str_contains('How are you', 'are')) { 
echo 'true';
}

RFC

Before PHP 8

You can use the strpos() function which is used to find the occurrence of one string inside another one:

$haystack = 'How are you?';
$needle = 'are';

if (strpos($haystack, $needle) !== false) {
echo 'true';
}

Note that the use of !== false is deliberate (neither != false nor === true will return the desired result); strpos() returns either the offset at which the needle string begins in the haystack string, or the boolean false if the needle isn't found. Since 0 is a valid offset and 0 is "falsey", we can't use simpler constructs like !strpos($a, 'are').

How can I check if a word is contained in another string using PHP?

You have a few options depending on your needs. For this simple example, strpos() is probably the simplest and most direct function to use. If you need to do something with the result, you may prefer strstr() or preg_match(). If you need to use a complex pattern instead of a string as your needle, you'll want preg_match().

$needle = "to";
$haystack = "I go to school";

strpos() and stripos() method (stripos() is case insensitive):

if (strpos($haystack, $needle) !== false) echo "Found!";

strstr() and stristr() method (stristr is case insensitive):

if (strstr($haystack, $needle)) echo "Found!";

preg_match method (regular expressions, much more flexible but runs slower):

if (preg_match("/to/", $haystack)) echo "Found!";

Because you asked for a complete function, this is how you'd put that together (with default values for needle and haystack):

function match_my_string($needle = 'to', $haystack = 'I go to school') {
if (strpos($haystack, $needle) !== false) return true;
else return false;
}

PHP 8.0.0 now contains a str_contains function that works like so:

if (str_contains($haystack, $needle)) {
echo "Found";
}

How to check if a string contains a specific text

Use the strpos function: http://php.net/manual/en/function.strpos.php

$haystack = "foo bar baz";
$needle = "bar";

if( strpos( $haystack, $needle ) !== false) {
echo "\"bar\" exists in the haystack variable";
}

In your case:

if( strpos( $a, 'some text' ) !== false ) echo 'text';

Note that my use of the !== operator (instead of != false or == true or even just if( strpos( ... ) ) {) is because of the "truthy"/"falsy" nature of PHP's handling of the return value of strpos.

As of PHP 8.0.0 you can now use str_contains

<?php
if (str_contains('abc', '')) {
echo "Checking the existence of the empty string will always
return true";
}

PHP string contains


PHP 8 or newer:

Use the str_contains function.

if (str_contains($str, "."))
{
echo 'Found it';
}

else
{
echo 'Not found.';
}

PHP 7 or older:

if (strpos($str, '.') !== FALSE)
{
echo 'Found it';
}

else
{
echo 'Not found.';
}

Note that you need to use the !== operator. If you use != or <> and the '.' is found at position 0, the comparison will evaluate to true because 0 is loosely equal to false.

Check if string contains one of several words

To extend your current code you could use an array of target words to search for, and use a loop:

$a = 'How are you ?';

$targets = array('How', 'are');

foreach($targets as $t)
{
if (strpos($a,$t) !== false) {
echo 'one of the targets was found';
break;
}
}

Keep in mind that the use of strpos() in this way means that partial word matches can be found. For example if the target was ample in the string here is an example then a match will be found even though by definition the word ample isn't present.

For a whole word match, there is an example in the preg_match() documentation that can be expanded by adding a loop for multiple targets:

foreach($targets as $t)
{
if (preg_match("/\b" . $t . "\b/i", $a)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
}

How to check if string contains two or more specific words

that is one possible solution

if(preg_match("/(?=.* s )(?=.*g898)(?=.*w63)/i", $string)){
echo 'found';
}


Related Topics



Leave a reply



Submit