strrchr

strrchr(string $haystack, string $needle, bool $before_needle = false): string|false

Take from the LAST occurrence to the end (the reverse-direction mirror of strstr); before_needle (8.3+) flips to the head; false when not found.

C C23

C23
/* strrchr(3): the C original - pointer to the LAST occurrence,
   so pointer arithmetic to the end gives the strstr-reversed tail. */
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *tail = strrchr("report.2024.final.pdf", '.');
    printf("%s\n", tail);                       /* ".pdf" */
    printf("%s\n", strrchr("a/b/c.txt", '/'));  /* "/c.txt" */
    printf("%d\n", strrchr("no-suffix", '.') == NULL);
    /* before_needle = everything up to (not including) the last hit */
    const char *s = "user@example.com";
    char head[32]; /* zero-initialized buffer */
    memset(head, 0, sizeof(head));
    const char *at = strrchr(s, '@');
    memcpy(head, s, (size_t)(at - s));
    printf("%s\n", head);
    return 0;
}
Output
.pdf
/c.txt
1
user

C++ C++26

C++26
// std::string::rfind + substr: the C++ spelling of strrchr (and its
// before_needle head via the position).
#include <iostream>
#include <string>

int main() {
    std::string s = "report.2024.final.pdf";
    auto p = s.rfind('.');
    std::cout << (p == std::string::npos ? "miss" : s.substr(p)) << "\n";
    std::cout << std::boolalpha << (p != std::string::npos) << "\n";
    std::string email = "user@example.com";
    auto at = email.rfind('@');
    std::cout << email.substr(at) << "\n";   // tail
    std::cout << email.substr(0, at) << "\n"; // head = before_needle
}
Output
.pdf
true
@example.com
user

C# v11.0.100

v11.0.100
// LastIndexOf + range: the strrchr split (tail by default, head as before_needle).
var s = "report.2024.final.pdf";
int p = s.LastIndexOf('.');
Console.WriteLine(p >= 0 ? s[p..] : "miss");
Console.WriteLine("a/b/c.txt"[("a/b/c.txt").LastIndexOf('/')..]);
Console.WriteLine("no-suffix".LastIndexOf('.') < 0);
var email = "user@example.com";
int at = email.LastIndexOf('@');
Console.WriteLine(email[at..]);
Console.WriteLine(email[..at]);
Output
.pdf
/c.txt
True
@example.com
user

Go v1.27.4

v1.27.4
// strings.LastIndex gives the byte offset of the last hit; two slices
// reproduce strrchr and its before_needle head.
package main

import (
	"fmt"
	"strings"
)

func main() {
	s := "report.2024.final.pdf"
	p := strings.LastIndex(s, ".")
	if p >= 0 {
		fmt.Println(s[p:])
	} else {
		fmt.Println("miss")
	}
	fmt.Println(strings.LastIndex("no-suffix", ".") < 0)
	email := "user@example.com"
	at := strings.LastIndex(email, "@")
	fmt.Println(email[at:])
	fmt.Println(email[:at])
}
Output
.pdf
true
@example.com
user

Java v27.0.1

v27.0.1
// lastIndexOf + substring: strrchr (tail) and before_needle (head).
public class Main {
    public static void main(String[] args) {
        String s = "report.2024.final.pdf";
        int p = s.lastIndexOf('.');
        System.out.println(p >= 0 ? s.substring(p) : "miss");
        System.out.println("no-suffix".lastIndexOf('.') < 0);
        String email = "user@example.com";
        int at = email.lastIndexOf('@');
        System.out.println(email.substring(at));
        System.out.println(email.substring(0, at));
    }
}
Output
.pdf
true
@example.com
user

JavaScript v26.13.1

v26.13.1
// lastIndexOf + slice: strrchr (tail) and before_needle (head).
const s = "report.2024.final.pdf";
const p = s.lastIndexOf(".");
console.log(p >= 0 ? s.slice(p) : "miss");
console.log("no-suffix".lastIndexOf(".") < 0);
const email = "user@example.com";
const at = email.lastIndexOf("@");
console.log(email.slice(at));
console.log(email.slice(0, at));
Output
.pdf
true
@example.com
user

PHP v8.6.0

v8.6.0
// strrchr - from the LAST occurrence of needle to the end of haystack
// (the reverse-direction mirror of strstr). before_needle (8.3+) flips it
// to the head instead. false when the needle never occurs.
var_dump(strrchr("report.2024.final.pdf", ".")); // last dot -> ".pdf"
var_dump(strrchr("a/b/c.txt", "/"));             // "/c.txt"
var_dump(strrchr("no-suffix", ".") === false);
var_dump(strrchr("user@example.com", "@", true)); // before_needle -> "user"
Output
string(4) ".pdf"
string(6) "/c.txt"
bool(true)
string(4) "user"

Python v3.15.2

v3.15.2
# rfind + slicing: the strrchr tail and its before_needle head.
s = "report.2024.final.pdf"
p = s.rfind(".")
print(s[p:] if p >= 0 else "miss")
print("no-suffix".rfind(".") < 0)
email = "user@example.com"
at = email.rfind("@")
print(email[at:])
print(email[:at])
Output
.pdf
True
@example.com
user

Rust v1.99.1

v1.99.1
// rfind returns the byte index of the last hit; slicing both ways gives
// the strrchr tail and the before_needle head.
fn main() {
    let s = "report.2024.final.pdf";
    let p = s.rfind('.');
    match p {
        Some(i) => println!("{}", &s[i..]),
        None => println!("miss"),
    }
    println!("{}", "no-suffix".rfind('.').is_none());
    let email = "user@example.com";
    let at = email.rfind('@').unwrap();
    println!("{}", &email[at..]);
    println!("{}", &email[..at]);
}
Output
.pdf
true
@example.com
user

TypeScript v26.13.1

v26.13.1
// Same as JavaScript: lastIndexOf + slice.
const s: string = "report.2024.final.pdf";
const p: number = s.lastIndexOf(".");
console.log(p >= 0 ? s.slice(p) : "miss");
console.log("no-suffix".lastIndexOf(".") < 0);
const email: string = "user@example.com";
const at: number = email.lastIndexOf("@");
console.log(email.slice(at));
console.log(email.slice(0, at));
Output
.pdf
true
@example.com
user

Language differences

C: strrchr(3) IS the C original (<string.h>) - the "hand-write" note in the task list was a misjudgment; C has the same-family builtin, so it is demonstrated directly; it returns a pointer, pointer arithmetic gives the offset, NULL plays false. Note C strrchr takes an int (single char) needle, while PHP takes a full-string needle (multi-char needles and before_needle fully supported since PHP 8.3; before that only the first char was used). C++: rfind + substr (npos means miss). C#: LastIndexOf + range slicing s[p..] / s[..p]. Go: strings.LastIndex. Java: lastIndexOf + substring. JavaScript/TypeScript: lastIndexOf + slice. PHP: mirrors strstr (first-occurrence direction); typical uses are extension extraction (strrchr($file, ".")) and email domains; before_needle (8.3+) saves a substr; an empty needle returns the whole haystack since 8.0 (not false); false when missing. Python: rfind plus slicing. Rust: rfind returns Option<usize>, sliced both ways. Teaching anchor: every language shares the "search-from-the-right + slice both ways" mental model - PHP is the only one bundling tail/head into ONE function via ONE parameter (before_needle); everyone else makes the caller cut twice; for extension extraction beware that "archive.tar.gz" yields ".gz".