strstr

(PHP 4, PHP 5, PHP 7)

strstrFind the first occurrence of a string

Descrierea

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.

Notă:

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

Notă:

If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead.

Parametri

haystack

The input string.

needle

Dacă needle nu este un șir de caractere, el este transformat în întreg și aplicat ca valoarea ordinală a caracterului. Acest comportament este învechit începând cu PHP 7.3.0 și utilizarea lui este foarte nerecomandată. În dependență de comportamentul dorit needle trebuie transformat în mod explicit în șir de caractere, sau trebuie efectuat un apel explicit către chr().

before_needle

If true, strstr() returns the part of the haystack before the first occurrence of the needle (excluding the needle).

Valorile întoarse

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

Istoricul schimbărilor

Versiune Descriere
8.0.0 Passing an int as needle is no longer supported.
7.3.0 Passing an int as needle has been deprecated.

Exemple

Example #1 strstr() example

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

$user strstr($email'@'true); // As of PHP 5.3.0
echo $user// prints name
?>

A se vedea și

  • 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() - Search a string for any of a set of characters
  • preg_match() - Perform a regular expression match