PHP stripos() Function: Case‑Insensitive Search for Substring Position

This article explains how PHP's stripos() function performs a case‑insensitive search to locate the first occurrence of a substring within a string, detailing its parameters, return values, and providing a complete example with expected output.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP stripos() Function: Case‑Insensitive Search for Substring Position

The stripos() function in PHP finds the position of the first occurrence of a substring within a string without regard to case.

Parameters: $haystack – the string to search in. $needle – the substring to look for; if not a string it is converted to an integer. $offset (optional) – the position in $haystack to start searching from; the returned index is still relative to the start of the string.

Return value: The numeric position of $needle in $haystack (starting at 0) or FALSE if the needle is not found.

Example:

<?php
$findme = 'a';
$mystring1 = 'xyz';
$mystring2 = 'ABC';
$pos1 = stripos($mystring1, $findme);
$pos2 = stripos($mystring2, $findme);
if ($pos1 === false) {
    echo "The string '$findme' was not found in the string '$mystring1'";
}
if ($pos2 !== false) {
    echo "We found '$findme' in '$mystring2' at position $pos2";
}
?>

The script outputs a message indicating that the character ‘a’ was not found in “xyz”, and that it was found at position 0 in “ABC”.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backendcase-insensitivestring-functionsstriposphp-tutorial
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.