Hàm mb_strrchr() tìm xuất hiện cuối cùng của needle trong haystack. Phân biệt chữ hoa/thường (case-sensitive). Trả về phần haystack từ vị trí đó (tùy before_needle). Trả về false nếu không tìm thấy.
Cú pháp
string|false mb_strrchr(
string $haystack,
string $needle,
bool $before_needle = false,
string $encoding = null
)
Tham số
| Tham số | Mô tả |
|---|---|
$haystack |
Chuỗi gốc. |
$needle |
Chuỗi con cần tìm. |
$before_needle |
true: phần từ đầu đến trước needle. false (mặc định): phần từ needle đến cuối (kể needle). |
$encoding |
Encoding ký tự. Null = internal encoding. |
Ví dụ 1: before_needle = false
<?php
$str = mb_strrchr("PHP Tutorials", "ia", false);
print_r($str); // "ials"
?>
Ví dụ 2: before_needle = true
<?php
$str = mb_strrchr("PHP Tutorials", "ia", true);
print_r($str); // "PHP Tutor"
?>
Ví dụ 3: Case-sensitive – không tìm thấy
<?php
$str = mb_strrchr("PHP Tutorials", "Ia", false); // "Ia" viết hoa
print_r($str); // "" (empty string) hoặc false
?>
Lưu ý: Do case-sensitive, needle "Ia" không khớp với "ia" trong haystack → trả về false.
So sánh
| Hàm | Case-sensitive | Tìm |
|---|---|---|
mb_strrchr() |
Có | Cuối cùng |
mb_strrichr() |
Không | Cuối cùng |
mb_strstr() |
Có | Đầu tiên |
mb_stristr() |
Không | Đầu tiên |