What are some built-in PHP functions for checking the presence of a substring in a string?
When working with strings in PHP, it is common to need to check if a particular substring exists within a larger string. PHP provides several built-in functions that can be used to accomplish this, such as strpos(), strstr(), and preg_match(). These functions return the position of the substring within the string, or a boolean value indicating whether the substring was found.
$string = "Hello, world!";
$substring = "world";
// Using strpos()
if (strpos($string, $substring) !== false) {
echo "Substring found using strpos()";
}
// Using strstr()
if (strstr($string, $substring)) {
echo "Substring found using strstr()";
}
// Using preg_match()
if (preg_match("/$substring/", $string)) {
echo "Substring found using preg_match()";
}