A string is a collection of given characters and a substring is a string present in a given string.
In this article, we are going to check if the given string contains a substring by using the PHP strpos() function.
Syntax:
strpos($original_string,$sub_string);
Parameters:
- original_string : The input string
- sub_string : The substring searched in the original input string.
Return type: Boolean value
- True, If substring found
- False, If substring not found.
Example:
PHP
<?php
$input_string = "sravan kumar author at geeks for geeks " ;
$sub = "geeks" ;
if ( strpos ( $input_string , $sub ) !== false)
{
echo "True" ;
}
else
{
echo "False" ;
}
?>
|
Output:
True
Example 2:
PHP
<?php
$input_string = "sravan kumar author at geeks for geeks " ;
$sub = "computer" ;
if ( strpos ( $input_string , $sub ) !== false)
{
echo "True" ;
}
else
{
echo "False" ;
}
?>
|
Output:
False
Example 3: The following example also considers space.
PHP
<?php
$input_string = "geeks for geeks java python php" ;
$sub = " " ;
if ( strpos ( $input_string , $sub ) !== false)
{
echo "True" ;
}
else
{
echo "False" ;
}
?>
|
Output:
True
Example 4:
PHP
<?php
$input_string = "geeks for geeks java python php" ;
$sub = " dbms" ;
if ( strpos ( $input_string , $sub ) !== false)
{
echo "True" ;
}
else
{
echo "False" ;
}
?>
|
Output:
False
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!