Skip to content
PHP Logo

What is the split function in PHP and how can it be used to search for a substring within a string?

The split function in PHP is used to split a string into an array based on a specified delimiter. To search for a substring within a string using the split function, you can split the string using the substring as the delimiter and then check if the resulting array has more than one element.

php
$string = "Hello, world!";
$substring = "world";

// Split the string using the substring as the delimiter
$parts = explode($substring, $string);

// Check if the resulting array has more than one element
if(count($parts) > 1) {
    echo "Substring found in the string.";
} else {
    echo "Substring not found in the string.";
}