Pre-requisite:
The scripts used in the examples of this tutorial are written based on the PHP 8 version. Do the following task before executing the examples of this script.
- Install apache2 and PHP 8.
- Set execute permission for all files and folders under /var/www/html folder where all PHP files will be stored.
Use of strpos() Function
The strpos() function is used to find out the position of the first occurrence of the substring in the main string. The syntax of this function is given below.
The first argument and the second argument of this function are mandatory arguments and the third argument is optional. The first argument takes the main string value and the second argument takes the search string value. The third argument takes the starting position of the argument. It returns the position value if the substring exists in the main string otherwise returns False.
Example-1: Search substring using strpos() function
Create a PHP file with the following script to search a substring in a string from the starting of the string and the particular position of the string. Here, the search string or substring and the starting position to start searching will be given the URL. The $_GET[] array has been used to read the value of the URL query parameters named ‘search’ and ‘pos’.
//Define a string value
$string = "Welcome to LinuxHint.";
//Check search string has given in the URL or not
if(isset($_GET['search']))
{
$search_str = $_GET['search'];
//Check position value has given in the URL or not
if(isset($_GET['pos']))
{
$position = $_GET['pos'];
//Find the position of the search string after particular position
if (strpos($string, $search_str, $position) !== False)
echo "The $search_str found at position ".strpos($string, $search_str, $position);
else
echo "The string doesn't contain the string.";
}
//Find the position of the search string
elseif (strpos($string, $search_str) !== False)
{
echo "The $search_str found at position ".strpos($string, $search_str);
}
else
//Print message if the position of the search word is not found
echo "The string doesn't contain the string.";
}
else
//Print message if no searching string has been given in the URL
echo "No searching string found."
?>
Output:
The following output will appear after executing the above script without any query parameter. Here, the filename is substr1.php that is stored inside /var/www/html/code folder.
http://localhost/code/substr1.php
The following output will appear after executing the above script with the query parameter named ‘search’.
http://localhost/code/substr1.php?search=Linux
The following output will appear after executing the above script with the query parameters named ‘search’ and ‘pos’.
http://localhost/code/substr1.php?search=com&pos=2
The following output will appear after executing the above script with the query parameters named ‘search’ and ‘pos’.
http://localhost/code/substr1.php?search=com&pos=5
Use of strstr() Function
The strstr() function is used to search a substring into a string and returns the string starts from the position of the substring to the end of the main string if the substring exists in the main string. The syntax of the strstr() function is given below.
The first argument and the second argument of this function are mandatory arguments and the third argument is optional. The first argument takes the main string value and the second argument takes the substring value. The default value of the third argument is False. If the third argument is set to True, then the function will return a string starting from the beginning of the main string to the position where the substring is found.
Example-2: Search substring using strstr() function
Create a PHP file with the following script to search a substring in a string using strstr() function. Here, the substring will be given the URL. The $_GET[] array has been used to read the value of the URL query parameter named ‘search’.
//Define a string value
$main_str = "PHP is a server-side scripting language.";
//Check search string has given in the URL or not
if(isset($_GET['search']))
{
$search_str = $_GET['search'];
//Find the search string exists in the main string or not
if(strstr($main_str, $search_str) != false)
{
$output = strstr($main_str, $search_str);
echo "The main string contains the search word, <b>$search_str</b>.<br />";
echo "Search output: $output";
}
}
else
//Print message if no searching string has been given in the URL
echo "No searching string found."
?>
Output:
The following output will appear after executing the above script with the query parameter named ‘search’ with the value ‘server’. Here, the filename is substr2.php that is stored inside /var/www/html/code folder.
http://localhost/code/substr2.php?search=server
Use of str_contains() Function
The str_contains() function exists in PHP 8 version only. It is used to check a substring exists in the main string or not. The syntax of this function is given below.
The first argument and the second argument of this function are mandatory arguments. The first argument takes the main string value and the second argument takes the search string value. It returns True if the substring exists in the main string otherwise returns False.
Example-3: Search substring using str_contains() function
Create a PHP file with the following script to search a substring in a string using the str_contains() function. Here, the substring will be given the URL. The $_GET[] array has been used to read the value of the URL query parameter named ‘search’.
//Define a string value
$string = 'HTML is a mark-up language.';
//Check search string has given in the URL or not
if(isset($_GET['search']))
{
$search_str = $_GET['search'];
echo "<b>The main string:</b><br />$string<br />";
echo "<b>The search string:</b><br />$search_str<br />";
//Check the search string exists in the main string or not
if(str_contains($string, $search_str))
echo "<br />The search string exist in the main string";
else
echo "<br />The search string doesn't exist in the main string.";
}
?>
Output:
The following output will appear after executing the above script with the query parameter named ‘search’ with the value ‘language’. Here, the filename is substr3.php that is stored inside /var/www/html/code folder.
http://localhost/code/substr3.php?search=language
The following output will appear after executing the above script with the query parameter named ‘search’ with the value ‘script’.
http://localhost/code/substr3.php?search=script
Conclusion
The uses of three built-in functions have been described in this tutorial to check a substring exists in another string or not. I hope the PHP users will be able to use any of the functions mentioned here to search the substring based on their requirements after reading this tutorial.