How do I check if a string contains a substring

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

Here is an example:

<?php $string = "Hello, welcome to OpenAI!"; $substring = "welcome"; if (strpos($string, $substring) !== false) { echo "The substring '$substring' was found in the string."; } else { echo "The substring '$substring' was not found in the string."; } ?>

String Substring PHP strpos Check