PHP stripos() Function: Case‑Insensitive Search for a Substring Position
This article explains the PHP stripos() function, which performs a case‑insensitive search for the first occurrence of a needle string within a haystack string, describes its parameters and return values, and provides a complete example with code illustrating typical usage and edge‑case handling.
The stripos() function in PHP finds the position of the first occurrence of a needle string inside a haystack string without regard to case.
Syntax:
int stripos(string $haystack, string $needle [, int $offset = 0])Description: It returns the numeric position where $needle first appears in $haystack. The search can start at an optional $offset, but the returned position is always relative to the beginning of $haystack.
Parameters:
haystack : The string to be searched.
needle : The string (or single character) to look for; non‑string values are cast to integers.
offset (optional): The character position in $haystack where the search should begin.
Return value: The function returns the position of $needle in $haystack (starting at 0). If the needle is not found, it returns FALSE.
Example:
<?php
$findme = 'a';
$mystring1 = 'xyz';
$mystring2 = 'ABC';
$pos1 = stripos($mystring1, $findme);
$pos2 = stripos($mystring2, $findme);
// $pos1 will be FALSE because 'a' is not in 'xyz'
if ($pos1 === false) {
echo "The string '$findme' was not found in the string '$mystring1'";
}
// $pos2 will be 0 because 'a' (case‑insensitive) is at the start of 'ABC'
if ($pos2 !== false) {
echo "We found '$findme' in '$mystring2' at position $pos2";
}
?>The example demonstrates that a strict identity check ( ===) is required to distinguish a valid position of 0 from FALSE, and shows how stripos() behaves with different case variations.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
