How to search text using php if ($text contains "World")

You can use the strpos() function in PHP to search for a specific string within another string. The strpos() 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 the strpos() function to check if the string "World" is present within the variable $text:

<?php
// $text is a string variable
$text = "Hello World!";

// Using the strpos() function to check if "World" exists in $text
if (strpos($text, "World") !== false) {
    // If strpos() returns a value other than false, it means "World" is found in $text
    echo "Found 'World' in '$text'";
} else {
    // If strpos() returns false, it means "World" is not found in $text
    echo "Could not find 'World' in '$text'";
}

Watch a course Learn object oriented PHP

Alternatively, you can use the preg_match() function for more complex pattern matching.

<?php
// $text is a string variable
$text = "Hello World!";

// Using the preg_match() function to check if "World" exists in $text
if (preg_match('/World/', $text)) {
    // If preg_match() returns a value other than 0, it means "World" is found in $text
    echo "Found 'World' in '$text'";
} else {
    // If preg_match() returns 0, it means "World" is not found in $text
    echo "Could not find 'World' in '$text'";
}

It's worth noting that the strpos() function is case-sensitive. If you want to perform a case-insensitive search, you can use stripos() instead.