<?PHP
echo strpos("This is a PHP tutorial","PHP"); //10
echo strpos("The best time is no time at that time","time"); //9
if (strpos("The best time is no time at that time","PHP"))
{
echo "contain PHP";
}
else echo "do not contain PHP";
//do not contain PHP
echo strpos("This is a PHP tutorial","This"); //0
?>
If specify a offset, then the first occurrence position of the 2nd string inside the 1st string after the offset will be return.
<?PHP
echo strpos("The best time is no time at that time","time", 10); //20
?>
If the substring starts from the first character, it will return 0. Since 0 is equal to false, === and !== may be used in a comparison to ensure correct results.
<?PHP
$str = "This is a PHP tutorial";
if (strpos($str, "This") == false)
{
echo "This not found."; //This not found
}
if (strpos($str, "This") === false)
{
echo "This not found.";
}
if (strpos($str, "What") === false)
{
echo "What not found."; //What not found
}
?>
<?PHP
$str = "This is a PHP tutorial";
if (stripos($str, "php") !== false)
{
echo "PHP found."; //php found
}
else
{
echo "PHP not found.";
}
?>
Similar function strrpos() finds the last occurrence of a substring.