strrpos / strripos
strrpos(string $haystack, string $needle, int $offset = 0): int|false — strripos(string $haystack, string $needle, int $offset = 0): int|false
Finds the position of the LAST occurrence of a substring (the reverse-search member of the strpos family); strripos is its case-insensitive variant. Returns false (not -1) on miss, and a match at position 0 needs !== false.
C C23
#include <stdio.h>
#include <string.h>
int main(void) {
const char *s = "hello world hello";
// C has no strrstr: find every hit with strstr and keep the last one
const char *last = NULL;
for (const char *p = s; (p = strstr(p, "hello")) != NULL; ++p) {
last = p; // advance one byte each round to allow overlapping matches
}
printf("%ld\n", last != NULL ? (long)(last - s) : -1L); // 12
// single character: strrchr IS the standard reverse lookup
printf("%ld\n", (long)(strrchr(s, 'w') - s)); // 6
return 0;
}
12 6
C++ C++26
#include <iostream>
#include <string>
int main() {
std::string s = "hello world hello";
std::cout << s.rfind("hello") << "\n"; // 12: the LAST occurrence
std::cout << s.rfind('w') << "\n"; // 6: single-char overload, like strrchr
std::cout << (s.rfind("xyz") == std::string::npos) << "\n"; // 1: npos means not found
return 0;
}
12 6 1
C# v11.0.100
string s = "hello world hello";
Console.WriteLine(s.LastIndexOf("hello")); // 12
Console.WriteLine(s.LastIndexOf('w')); // 6
Console.WriteLine(s.LastIndexOf("xyz")); // -1: not found
// case-insensitive reverse: no built-in — compare a lower-cased copy
12 6 -1
Go v1.27.4
package main
import (
"fmt"
"strings"
)
func main() {
s := "hello world hello"
fmt.Println(strings.LastIndex(s, "hello")) // 12
fmt.Println(strings.LastIndexByte(s, 'w')) // 6: single byte, like strrchr
fmt.Println(strings.LastIndex(s, "xyz")) // -1: not found
// any-of-a-set reverse search: LastIndexAny
fmt.Println(strings.LastIndexAny(s, "lo")) // 16
}
12 6 -1 16
Java v27.0.1
public class Main {
public static void main(String[] args) {
String s = "hello world hello";
System.out.println(s.lastIndexOf("hello")); // 12
System.out.println(s.lastIndexOf('w')); // 6
System.out.println(s.lastIndexOf("xyz")); // -1: not found
// case-insensitive reverse has no built-in: lower-case both sides first
}
}
12 6 -1
JavaScript v26.13.1
const s = "hello world hello"
console.log(s.lastIndexOf("hello")) // 12
console.log(s.lastIndexOf("w")) // 6
console.log(s.lastIndexOf("xyz")) // -1: not found
12 6 -1
PHP v8.6.0
$s = "hello world hello";
var_dump(strrpos($s, "hello")); // 12: the LAST occurrence
var_dump(strripos($s, "WORLD")); // 6: case-insensitive reverse search
var_dump(strrpos($s, "xyz") === false); // not found returns false, not -1
int(12) int(6) bool(true)
Python v3.15.2
s = "hello world hello"
print(s.rfind("hello")) # 12
print(s.rfind("w")) # 6
print(s.rfind("xyz")) # -1: not found
12 6 -1
Rust v1.99.1
fn main() {
let s = "hello world hello";
println!("{:?}", s.rfind("hello")); // Some(12): the LAST occurrence
println!("{:?}", s.rfind('w')); // Some(6)
println!("{:?}", s.rfind("xyz")); // None: not found (PHP returns false here)
}
Some(12) Some(6) None
TypeScript v26.13.1
const s: string = "hello world hello";
console.log(s.lastIndexOf("hello")); // 12
console.log(s.lastIndexOf("xyz")); // -1: not found
12 -1
Language differences
C: no strrstr (multi-char reverse search is not standard) - the example loops with strstr and keeps the last hit; single-char reverse lookup is strrchr.
C++: rfind is the native reverse member; not-found is npos (the example compares against it).
C#: LastIndexOf returns -1 on miss.
Go: LastIndex returns -1 on miss; LastIndexByte corresponds to strrchr; LastIndexAny is the any-of-a-set reverse variant.
Java: lastIndexOf returns -1 on miss.
JavaScript: lastIndexOf returns -1 on miss.
PHP: strrpos returns false on miss (not -1); a positive $offset restricts the search window to the right of it, a negative one counts from the end - the direction is easy to misread; strripos is the case-insensitive variant, use mb_strripos for multibyte strings.
Python: rfind returns -1 on miss.
Rust: rfind returns Option<usize> - None on miss.
TypeScript: same as JavaScript.