PHP string "contains"

To check if a string contains another string in PHP, you can use the strpos() function. This function returns the position of the first occurrence of a substring within a string, or FALSE if the substring is not found.

Here's an example of how you can use strpos() to check if a string contains another string:

<?php

$string = 'The quick brown fox jumps over the lazy dog';
if (strpos($string, 'fox') !== false) {
    echo 'The string contains the word fox';
} else {
    echo 'The string does not contain the word fox';
}

This will output The string contains the word fox. If you want to check if the string contains a case-insensitive version of the substring, you can use the stripos() function instead of strpos().

Watch a course Learn object oriented PHP

You can also use the strstr() function to check if a string contains another string. This function returns the portion of the string from the first occurrence of the substring to the end of the string, or FALSE if the substring is not found.

Here's an example of how you can use strstr() to check if a string contains another string:

<?php

$string = 'The quick brown fox jumps over the lazy dog';
if (strstr($string, 'fox')) {
    echo 'The string contains the word fox';
} else {
    echo 'The string does not contain the word fox';
}

This will also output The string contains the word fox.