strstr

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

strstrFind the first occurrence of a string

Descrição

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

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

Nota:

This function is case-sensitive. For case-insensitive searches, use stristr().

Nota:

If it is only required to determine if a particular needle occurs within haystack, the faster and less memory intensive str_contains() function should be used instead.

Parâmetros

haystack

The input string.

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

Valor Retornado

Returns the portion of string, or false if needle is not found.

Registro de Alterações

VersãoDescrição
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 strstr() example

<?php
$email
= 'name@example.com';
$domain = strstr($email, '@');
echo
$domain; // prints @example.com

$user = strstr($email, '@', true);
echo
$user; // prints name
?>

Veja Também

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