stristr

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

stristrCase-insensitive strstr()

Descrição

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

Returns all of haystack starting from and including the first occurrence of needle to the end.

Parâmetros

haystack

The string to search in

needle

The string to search for.

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, stristr() returns the part of the haystack before the first occurrence of the needle (excluding needle).

needle and haystack are examined in a case-insensitive manner.

Valor Retornado

Returns the matched substring. If needle is not found, returns false.

Registro de Alterações

VersãoDescrição
8.2.0 A redução de todas as letras a maiúsculas ou minúsculas não depende mais da localidade definida com setlocale(). Somente a redução de todas as letras ASCII a maiúsculas ou minúsculas será feita. Os bytes não ASCII serão comparados por seu valor de byte.
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 stristr() example

<?php
$email
= 'USER@EXAMPLE.com';
echo
stristr($email, 'e'); // outputs ER@EXAMPLE.com
echo stristr($email, 'e', true); // outputs US
?>

Exemplo #2 Testing if a string is found or not

<?php
$string
= 'Hello World!';
if(
stristr($string, 'earth') === FALSE) {
echo
'"earth" not found in string';
}
// outputs: "earth" not found in string
?>

Exemplo #3 Using a non "string" needle

<?php
$string
= 'APPLE';
echo
stristr($string, 97); // 97 = lowercase a
// outputs: APPLE
?>

Notas

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

Veja Também

  • strstr() - Find the first occurrence of a string
  • strrchr() - Find the last occurrence of a character in a string
  • stripos() - Find the position of the first occurrence of a case-insensitive substring in a string
  • strpbrk() - Procura na string por um dos caracteres de um conjunto
  • preg_match() - Perform a regular expression match
To Top