array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'uk', ), 'this' => array ( 0 => 'function.strrchr.php', 1 => 'strrchr', 2 => 'Find the last occurrence of a character in a string', ), 'up' => array ( 0 => 'ref.strings.php', 1 => 'String Функції', ), 'prev' => array ( 0 => 'function.strpos.php', 1 => 'strpos', ), 'next' => array ( 0 => 'function.strrev.php', 1 => 'strrev', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/strings/functions/strrchr.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>
(PHP 4, PHP 5, PHP 7, PHP 8)
strrchr — Find the last occurrence of a character in a string
This function returns the portion of haystack
which
starts at the last occurrence of needle
and goes
until the end of haystack
.
haystack
The string to search in
needle
If needle
contains more than one character,
only the first is used. This behavior is different from that of
strstr().
До версії PHP 8.0.0, якщо параметр needle
не є рядком,
він перетворюється на ціле число і розглядається як код символу. Ця поведінка
є застарілою, починаючи з PHP 7.3.0. Вкрай не рекомендується на неї
покладатися. Залежно від потрібної поведінки, параметр
needle
необхідно привести до рядкового типу або
обробити функцією chr().
before_needle
If true
, strrchr()
returns the part of the haystack
before the
last occurrence of the needle
(excluding the needle).
This function returns the portion of string, or false
if
needle
is not found.
Версія | Опис |
---|---|
8.3.0 |
The before_needle parameter was added.
|
8.0.0 |
Тепер параметр needle може бути порожнім рядком.
|
8.0.0 |
Passing an int as needle is no longer supported.
|
7.3.0 |
Passing an int as needle has been deprecated.
|
Приклад #1 strrchr() example
<?php
$ext = strrchr('somefile.txt', '.');
echo "file extension: $ext \n";
$ext = $ext ? strtolower(substr($ext, 1)) : '';
echo "file extension: $ext";
?>
Поданий вище приклад виведе щось схоже на:
file extension: .txt file extension: txt
Зауваження: Ця функція є бінарно безпечною.