(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().
Antes do PHP 8.0.0, se needle
não for uma string, ela será convertida para um número inteiro e aplicada como o valor ordinal de um caractere. Este comportamento tornou-se defasado a partir do PHP 7.3.0 e depender dele é altamente desaconselhado. Dependendo do comportamento pretendido, o parâmetro needle
deve ser explicitamente convertido em string ou uma chamada explícita para chr() deve ser realizada.
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.
Versão | Descrição |
---|---|
8.3.0 | The before_needle parameter was added. |
8.0.0 | O parâmetro needle agora aceita uma string vazia. |
8.0.0 | Passing an int as needle is no longer supported. |
7.3.0 | Passing an int as needle has been deprecated. |
Exemplo #1 strrchr() example
<?php
$ext = strrchr('somefile.txt', '.');
echo "file extension: $ext \n";
$ext = $ext ? strtolower(substr($ext, 1)) : '';
echo "file extension: $ext";
?>
O exemplo acima produzirá algo semelhante a:
file extension: .txt file extension: txt
Nota: Esta função é compatível com dados binários.