PHP Program to check if a sub string is present in the string


April 22, 2022, Learn eTutorial
1219

How to check whether the string is present in the original string or not using PHP?

In this program, we are going to check that whether a string is present in the previously provided string. For this, we are using a built-in function strpos() that is used to find the occurrence of the string in the string it is basically case sensitive. To perform this program we first have to assign a sentence into the variable string or else we can also enter it to the variable. After that we have to accept the second string which is used to search into the variable ser. Then we have to check the condition 'strpos(string, ser)' if true then print The string is present in the original string otherwise print that the string is not present in the original string.

Syntax


strpos(string,find,start)
 

 

ALGORITHM

Step 1: Assign the original string into the variable string

Step 2: Accept the string to be searched from the user and assign it to the varaiable ser

Step 3: Check the condition 'strpos(string, ser)' if true then print The string is present in the original string otherwise print that the string is not present in the original string

PHP Source Code

                                          <?php
$string = "Hi! all welcome to learnetutorials.com";
echo "The original string is: \n$string \n";
$ser = readline("Enter the search: \n");
if (strpos($string, $ser)) {
    echo "Yes, The string is present in the original string";
} else {
    echo "No, The string is not present in the original string";
}
?>
                                      

OUTPUT

The original string is:
Hi! all welcome to learnetutorials.com
Enter the search:  learn
Yes, The string is present in the original string