How can the strpos function in PHP be utilized to search for a substring within a string and return true if found?
To search for a substring within a string and return true if found, you can use the strpos function in PHP. This function returns the position of the first occurrence of a substring within a string, or false if the substring is not found. You can then check if the result is not equal to false to determine if the substring exists in the string.
$string = "Hello, World!";
$substring = "World";
if (strpos($string, $substring) !== false) {
echo "Substring found!";
} else {
echo "Substring not found.";
}
Keywords
Related Questions
- How can PHP be used to extract and display individual values from a specific row in a CSV file?
- What is the best way to store multiple values from a database query in PHP without overwriting them?
- What potential issues can arise when deploying a PHP contact form from a local environment to a server?