PHP provides built-in functions to search for substrings within a string and determine their exact position. These functions are commonly used for validation, parsing, and conditional logic.
strpos() – Finds the first occurrence of a substringstrrpos() – Finds the last occurrence of a substringstripos() – Case-insensitive search (first occurrence)strripos() – Case-insensitive search (last occurrence)false is returned!== false) when checking results
// Find the first occurrence of a substring
<?php
$string = "Hello World";
$position = strpos($string, "World");
echo $position;
?>
The substring World starts at index 6 in the string Hello World.
// Find the last occurrence of a substring
<?php
$string = "Hello World World";
$position = strrpos($string, "World");
echo $position;
?>
The last occurrence of World begins at index 12.
// Demonstrates logical flow of substring checking
If string contains substring
Return position
Else
Return false
stripos() for user-entered textPHP exists in a sentencestripos()