Description
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 thehaystack
before the first occurrence of theneedle
(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($string, 97); // 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
- addcslashes
- addslashes
- bin2hex
- chop
- chr
- chunk_split
- convert_cyr_string
- convert_uudecode
- convert_uuencode
- count_chars
- crc32
- crypt
- echo
- explode
- fprintf
- get_html_translation_table
- hebrev
- hebrevc
- hex2bin
- html_entity_decode
- htmlentities
- htmlspecialchars_decode
- htmlspecialchars
- implode
- join
- lcfirst
- levenshtein
- localeconv
- ltrim
- md5_file
- md5
- metaphone
- money_format
- nl_langinfo
- nl2br
- number_format
- ord
- parse_str
- printf
- quoted_printable_decode
- quoted_printable_encode
- quotemeta
- rtrim
- setlocale
- sha1_file
- sha1
- similar_text
- soundex
- sprintf
- sscanf
- str_getcsv
- str_ireplace
- str_pad
- str_repeat
- str_replace
- str_rot13
- str_shuffle
- str_split
- str_word_count
- strcasecmp
- strchr
- strcmp
- strcoll
- strcspn
- strip_tags
- stripcslashes
- stripos
- stripslashes
- stristr
- strlen
- strnatcasecmp
- strnatcmp
- strncasecmp
- strncmp
- strpbrk
- strpos
- strrchr
- strrev
- strripos
- strrpos
- strspn
- strstr
- strtok
- strtolower
- strtoupper
- strtr
- substr_compare
- substr_count
- substr_replace
- substr
- trim
- ucfirst
- ucwords
- vfprintf
- vprintf
- vsprintf
- wordwrap
Коментарии
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
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".
<?php
function stristr_reverse($haystack, $needle) {
$pos = stripos($haystack, $needle) + strlen($needle);
return substr($haystack, 0, $pos);
}
$email = 'USER@EXAMPLE.com';
echo stristr_reverse($email, 'er');
// outputs USER
?>
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
}
?>
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