stristr

(PHP 4, PHP 5)

stristrCase-insensitive strstr()

Description

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

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

Parameters

haystack

The string to search in

needle

If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.

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.

Return Values

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

Changelog

Version Description
5.3.0 Added the optional parameter before_needle.
4.3.0 stristr() was made binary safe.

Examples

Example #1 stristr() example

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

Example #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
?>

Example #3 Using a non "string" needle

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

Notes

Note: This function is binary-safe.

See Also

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

Коментарии

There was a change in PHP 4.2.3 that can cause a warning message
to be generated when using stristr(), even though no message was
generated in older versions of PHP.

The following will generate a warning message in 4.0.6 and 4.2.3:
  stristr("haystack", "");
     OR
  $needle = "";  stristr("haystack", $needle);

This will _not_ generate an "Empty Delimiter" warning message in
4.0.6, but _will_ in 4.2.3:
  unset($needle); stristr("haystack", $needle);

Here's a URL that documents what was changed:
http://groups.google.ca/groups?selm=cvshholzgra1031224321%40cvsserver
2002-10-03 00:36:01
http://php5.kiev.ua/manual/ru/function.stristr.html
An example for the stristr() function:

<?php
$a 
"I like php";
if (
stristr("$a""LikE PhP")) {
print (
"According to \$a, you like PHP.");
}
?>

It will look in $a for "like php" (NOT case sensetive. though, strstr() is case-sensetive).

For the ones of you who uses linux.. It is similiar to the "grep" command.
Actually.. "grep -i".
2002-11-12 14:26:34
http://php5.kiev.ua/manual/ru/function.stristr.html
<?php

function stristr_reverse($haystack$needle) {
 
$pos stripos($haystack$needle) + strlen($needle);
  return 
substr($haystack0$pos);
}
$email 'USER@EXAMPLE.com';
echo 
stristr_reverse($email'er');
// outputs USER

?>
2005-06-05 04:02:25
http://php5.kiev.ua/manual/ru/function.stristr.html
Just been caught out by stristr trying to converting the needle from an Int to an ASCII value.

Got round this by casting the value to a string.

<?php
if( !stristr$file, (string) $myCustomer->getCustomerID()  ) ) {
 
// Permission denied
}
?>
2007-10-06 08:02:41
http://php5.kiev.ua/manual/ru/function.stristr.html
Автор:
I think there is a bug in php 5.3 in stristr with uppercase Ä containing other character

http://pastebin.com/5bP6uztY

if you search only with täry it works, but as soon as the word is tärylä  it does not. TÄRYL works fine
2014-05-23 13:33:40
http://php5.kiev.ua/manual/ru/function.stristr.html

    Поддержать сайт на родительском проекте КГБ