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
<code>int strripos ( string $haystack , string $needle [, int $offset = 0 ] )</code>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
<code>$str = "Hello World";
$pos = strripos($str, "o");
echo $pos; // outputs 7</code>This 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
<code>$str = "Hello World";
$pos = strripos($str, "o", 5);
echo $pos; // outputs 4</code>Starting the search at position 5, the last "o" is found at index 4.
Example 3: Case‑insensitive search
<code>$str = "Hello World";
$pos = strripos($str, "WORLD");
echo $pos; // outputs 6</code>Because strripos() ignores case, the substring "WORLD" is found at position 6.
Example 4: Substring not found
<code>$str = "Hello World";
$pos = strripos($str, "X");
var_dump($pos); // outputs bool(false)</code>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
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.