Returns all of haystack
starting from and including the first occurrence of needle
to the end.
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.
Returns the matched substring. If needle
is not found, returns false
.
Versão | Descriçã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. |
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
?>
Nota: Esta função é compatível com dados binários.