strrchr

(PHP 4, PHP 5, PHP 7, PHP 8)

strrchrFind the last occurrence of a character in a string

Descrição

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

This function returns the portion of haystack which starts at the last occurrence of needle and goes until the end of haystack.

Parâmetros

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).

Valor Retornado

This function returns the portion of string, or false if needle is not found.

Registro de Alterações

VersãoDescriçã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.

Exemplos

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

Notas

Nota: Esta função é compatível com dados binários.

Veja Também

  • strstr() - Find the first occurrence of a string
  • strrpos() - Find the position of the last occurrence of a substring in a string
To Top