PHP strripos() Function: Syntax, Parameters, Return Value, and Usage Examples
This article explains the PHP strripos() function, detailing its syntax, parameters, return behavior, and provides multiple code examples demonstrating case‑insensitive reverse string searching, offset usage, and handling of unfound substrings.
The strripos() function in PHP finds the position of the last occurrence of a substring within a string without case sensitivity, similar to strpos() but case‑insensitive.
strripos() Function Syntax
int strripos ( string $haystack , string $needle [, int $offset = 0 ] )Parameters: $haystack: The string to be searched. $needle: The substring to look for. $offset (optional): The position in the string to start searching from; default is 0.
The function returns the position of the last occurrence of $needle in $haystack, or false if the substring is not found.
strripos() Function Usage Examples
Example 1: Find the last occurrence of a character
$str = "Hello World";
$pos = strripos($str, "o");
echo $pos; // outputs 7This searches for the last "o" in $str, storing the position (7) in $pos and printing it.
Example 2: Find the last occurrence with a starting offset
$str = "Hello World";
$pos = strripos($str, "o", 5);
echo $pos; // outputs 4Starting the search at position 5, the last "o" is found at index 4.
Example 3: Case‑insensitive search
$str = "Hello World";
$pos = strripos($str, "WORLD");
echo $pos; // outputs 6Because strripos() ignores case, the substring "WORLD" is found at position 6.
Example 4: Substring not found
$str = "Hello World";
$pos = strripos($str, "X");
var_dump($pos); // outputs bool(false)If the substring does not exist, the function returns false.
Summary
The article provides a comprehensive guide to the PHP strripos() function, explaining its purpose, syntax, parameters, return values, and offering practical examples for common use cases such as reverse searching, offset handling, and case‑insensitive matching.
PHP Learning Recommendations
Vue3+Laravel8+Uniapp Beginner to Advanced Development Tutorial
Vue3+TP6+API Social E‑commerce System Development Course
Swoole From Beginner to Expert Course
Workerman+TP6 Real‑time Chat System Limited‑time Offer
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.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
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.
