PHP 7.1.3 Released


(PHP 4, PHP 5, PHP 7)

strstrTrouve la première occurrence dans une chaîne


string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )

Retourne une sous-chaîne de haystack, allant de la première occurrence de needle (incluse) jusqu'à la fin de la chaîne.


strstr() est sensible à la casse. Pour une fonctionnalité identique, mais insensible à la casse, reportez-vous à stristr().


Si vous voulez juste déterminer si un needle particulier se trouve dans la chaîne haystack, utilisez la fonction strpos() qui est plus rapide et qui consomme moins de ressources.

Liste de paramètres


La chaîne d'entrée.


Si needle n'est pas une chaîne, elle sera convertie en entier, et utilisée comme code ASCII du caractère correspondant.


Si vaut TRUE, strstr() retourne la partie de haystack avant la première occurrence de needle (needle étant exclus).

Valeurs de retour

Retourne la portion de la chaîne, ou FALSE si needle n'est pas trouvé.


Version Description
5.3.0 Ajout du paramètre optionnel before_needle.
4.3.0 strstr() est maintenant compatible avec les données binaires.


Exemple #1 Exemple avec strstr()

$domain strstr($email'@');
$domain// Affiche :

$user strstr($email'@'true); // Depuis PHP 5.3.0
echo $user// Affiche : name

Voir aussi

  • stristr() - Version insensible à la casse de strstr
  • strrchr() - Trouve la dernière occurrence d'un caractère dans une chaîne
  • strpos() - Cherche la position de la première occurrence dans une chaîne
  • strpbrk() - Recherche un ensemble de caractères dans une chaîne de caractères
  • preg_match() - Effectue une recherche de correspondance avec une expression rationnelle standard

add a note add a note

User Contributed Notes 8 notes

gruessle at gmail dot com
5 years ago
Been using this for years:

* @author : Dennis T Kaplan
* @version : 1.0
* Date : June 17, 2007
* Function : reverse strstr()
* Purpose : Returns part of haystack string from start to the first occurrence of needle
* $haystack = 'this/that/whatever';
* $result = rstrstr($haystack, '/')
* $result == this
* @access public
* @param string $haystack, string $needle
* @return string

function rstrstr($haystack,$needle)
substr($haystack, 0,strpos($haystack, $needle));

You could change it to:
rstrstr ( string $haystack , mixed $needle [, int $start] )

function rstrstr($haystack,$needle, $start=0)
substr($haystack, $start,strpos($haystack, $needle));

laszlo dot heredy at gmail dot com
3 years ago
strstr() is not a way to avoid type-checking with strpos().

If $needle is the last character in $haystack, and testing $needle as a boolean by itself would evaluate to false, then testing strstr() as a boolean will evaluate to false (because, if successful, strstr() returns the first occurrence of $needle along with the rest of $haystack).

('01234');  // found a zero
findZero('43210');  // did not find a zero
findZero('0');      // did not find a zero
findZero('00');     // found a zero
findZero('000');    // found a zero
findZero('10');     // did not find a zero
findZero('100');    // found a zero

function findZero($numberString) {
    if (
strstr($numberString, '0')) {
'found a zero';
    } else {
'did not find a zero';

Also, strstr() is far more memory-intensive than strpos(), especially with longer strings as your $haystack, so if you are not interested in the substring that strstr() returns, you shouldn't be using it anyway.

There is no PHP function just to check only _if_ $needle occurs in $haystack; strpos() tells you if it _doesn't_ by returning false, but, if it does occur, it tells you _where_ it occurs as an integer, which is 0 (zero) if $needle is the first part of $haystack, which is why testing if (strpos($needle, $haystack)===false) is the only way to know for sure if $needle is not part of $haystack.

My advice is to start loving type checking immediately, and to familiarize yourself with the return value of the functions you are using.

brett dot jr dot alton at gmail dot com
9 years ago
For the needle_before (first occurance) parameter when using PHP 5.x or less, try:

= 'php-homepage-20071125.png';
$needle = '-';
$result = substr($haystack, 0, strpos($haystack, $needle)); // $result = php
w3b_monk3y at yahoo dot com
8 years ago
If you want to emulate strstr's new before_needle parameter pre 5.3 strtok is faster than using strpos to find the needle and cutting with substr. The amount of difference varies with string size but strtok is always faster.
xslidian at lidian dot info
4 years ago
For those in need of the last occurrence of a string:

function strrstr($h, $n, $before = false) {
$rpos = strrpos($h, $n);
$rpos === false) return false;
$before == false) return substr($h, $rpos);
    else return
substr($h, 0, $rpos);
trent dot renshaw at objectst dot com dot au
1 year ago
> root at mantoru dot de

PHP makes this easy for you. When working with domain portion of email addresses, simply pass the return of strstr() to substr() and start at 1:

substr(strstr($haystack, '@'), 1);
root at mantoru dot de
9 years ago
Please note that $needle is included in the return string, as shown in the example above. This ist not always desired behavior, _especially_ in the mentioned example. Use this if you want everything AFTER $needle.

function strstr_after($haystack, $needle, $case_insensitive = false) {
$strpos = ($case_insensitive) ? 'stripos' : 'strpos';
$pos = $strpos($haystack, $needle);
    if (
is_int($pos)) {
substr($haystack, $pos + strlen($needle));
// Most likely false or null
return $pos;

// Example
$email = '';
$domain = strstr_after($email, '@');
$domain; // prints
leo dot nard at free dot fr
11 years ago
When encoding ASCII strings to HTML size-limited strings, sometimes some HTML special chars were cut.

For example, when encoding "��" to a string of size 10, you would get: "à&a" => the second character is cut.

This function will remove any unterminated HTML special characters from the string...

function cut_html($string)

    while (
$a = strstr($a, '&'))
$b=strstr($a, ';');
        if (!
substr($string, 0, strlen($string)-$nb);
To Top