To check if a string contains a specific substring, the ‘strlen’ and ‘strpos’ functions can be used. The ‘strlen’ gives the entire length of the string. The function ‘strpos’ finds the position wherein the substring occurs first in the string.
Example
<?php $str_1 = "thisisasample"; $str_2 = "asample"; if (strpos($str_1, $str_2) >= 0 && strpos($str_1, $str_2) < strlen($str_1)) echo("The substring is present within the string."); else echo("The substring is not present within the string."); ?>
Output
The substring is present within the string.
Two strings are defined and the position of the second string in the first string is checked with the help of the ‘strpos’ function and compared with the length of the first string. Relevant message is displayed on the console.