|
|
 |
stristr (PHP 3 >= 3.0.6, PHP 4, PHP 5) stristr --
Case-insensitive strstr()
Descriptionstring stristr ( string haystack, string needle )
Returns all of haystack from the first
occurrence of needle to the end.
needle and haystack
are examined in a case-insensitive manner.
If needle is not found, returns FALSE.
If needle is not a string, it is converted
to an integer and applied as the ordinal value of a character.
Example 1. stristr() example |
<?php
$email = 'USER@EXAMPLE.com';
echo stristr($email, 'e');
?>
|
|
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';
}
?>
|
|
Example 3. Using a non "string" needle |
<?php
$string = 'APPLE';
echo stristr($string, 97); ?>
|
|
Note: This function is
binary-safe.
See also
strstr(),
strrchr(),
substr(), and
ereg().
User Contributed Notes
stristr
stoyan at mail dot ebpw dot net
18-Mar-2005 07:59
Be careful never to use integer value as a needle - always convert it to string before use e.g. using strval() - otherwise you'll get messed up - for example te following:
if ($res=stristr('40', 52)) echo $res;
will return '40' as $res - simply 52 is the ASCII code of '4' that's in the beginning of our string.
Actually that's covered in the description of the function but you may miss to pay attention to it just like me=]
paulphp at springfrog dot com
01-Nov-2003 12:30
Regarding the problem (posted by Dan) of checking for a zero where the zero is at the end of a string, the following will work. Note that !== is used rather than != which doesn't work.
$total = "Your total is 0";
$srchstrng = "0";
if (stristr($total, $srchstrng) !== FALSE)
{
echo "you have nothing";
}
This will correctly output "you have nothing", indicating the zero was correctly identified as being in the $total string.
(Discovered after experimenting with comparison operators detailed on this page: http://www.php.net/manual/en/language.operators.comparison .)
sam_at_compasspointmedia.com
07-Dec-2002 04:29
This function is tricky! The problem lies when the haystack is a string and the needle is a number. I found out the hard way:
This expression:
echo $spy = stristr("James Bond 007","007");
will return "007":
but this will not:
echo $spy = stristr("James Bond 007",007);
because 007 is interpreted as a number. Tricky, right?
Even if the first parameter could be interpreted as a number, but the second parameter is in quotes, it won't work. For example:
echo stristr("555007",007);
will not return anything.
but this will by the way...
echo $spy = stristr(555007,"007");
Techdeck at Techdeck dot org
12-Nov-2002 02:26
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".
dpatton.at.confluence.org
02-Oct-2002 11:36
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
moenm@hotmail_com
29-Aug-2002 08:44
A slightly more efficient way of getting a files extension. There is no reason to use strrev().
function getext($f) {
$ext = substr($f, strrpos($f,".") + 1);
return $ext;
}
php dot net at CamelQuotesNO-SPAM-PLEASE dot com
04-Aug-2002 01:52
Use this function to return the number of files matching a certain type (extention) in a given folder:
Using the previous code in the function above, file extentions like .ext.inc.php will be counted as .php files
usage:
returns false if dir doesnt exist
or returns the number of files counted
DO NOT add '.' to extention you are searching for.
example usage:
$result = CountFiles("./images", "jpg");
if($result === false) die("dir does not exist!");
function CountFiles($dir, $type)
{
if(!($dh =@opendir("$dir")))
return false; // directory does not exist
// read the directory contents searching for "type" files
// and count how many are found:
$files = 0;
while ( ! ( ($fn = readdir($dh)) === false ) )
{
$f = strrev($fn);
$ext = substr($f, 0, strpos($f,"."));
$f_ext = strrev($ext);
if(( strcasecmp($f_ext, $type) == 0 )) $files++;
}
closedir($dh);
return $files;
}
| |