Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: stristr - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

strlen" width="11" height="7"/> <stripslashes
Last updated: Thu, 31 May 2007

view this page in

stristr

(PHP 4, PHP 5)

stristr — 大文字小文字を区別しない strstr()

説明

string stristr ( string $haystack, string $needle )

haystack において needle が最初に見つかった位置から最後までを 返します。 needle および haystack は大文字小文字を区別せずに評価されます。

needle がない場合、FALSE を返します。

needle が文字列でない場合、整数に変換され、 通常の文字列として適用されます。

例 2403. stristr() の例

<?php
  $email
= 'USER@EXAMPLE.com';
  echo
stristr($email, 'e');
// ER@EXAMPLE.com を出力する
?>

例 2404. 文字列が見つかるかどうかをテストする

<?php
  $string
= 'Hello World!';
  if(
stristr($string, 'earth') === FALSE) {
    echo
'"earth" not found in string';
  }
// 出力: "earth" not found in string
?>

例 2405. 非 "文字列" のneedle を使用する

<?php
  $string
= 'APPLE';
  echo
stristr($string, 97); // 97 = lowercase a
// 出力: APPLE
?>

注意: この関数はバイナリデータに対応しています。

strstr()strrchr()substr() および preg_match() も参照ください。



strlen" width="11" height="7"/> <stripslashes
Last updated: Thu, 31 May 2007
 
add a note add a note User Contributed Notes
stristr
daniel at dbks com ar
20-Aug-2007 12:00
i used the script of 'notepad', but with a little modification for comfortably usage ^^

<?
function stristr_reverse($haystack, $needle) {
 
$pos = stripos($haystack, $needle) + strlen($needle);
  return
substr($haystack, 0, $pos);
}

$email = 'USER@EXAMPLE.com';

$proceso1 = stristr_reverse($email, '@');

$emailuser = substr($proceso1,0,strlen($proceso1) -1);

echo
"Thanks $emailuser for Registering with.. blabla";

//Thanks USER for Registering with.. blabla
?>
art at awilton dot dotcom
08-Nov-2005 02:17
handy little bit of code I wrote to take arguments from the command line and parse them for use in my apps.

<?php

 $i
= implode(" ",$argv); //implode all the settings sent via clie
 
$e = explode("-",$i); // no lets explode it using our defined seperator '-'

       //now lets parse the array and return the parameter name and its setting
       // since the input is being sent by the user via the command line
       //we will use stristr since we don't care about case sensitivity and
       //will convert them as needed later.

   
while (list($index,$value) = each($e)){

      
//lets grap the parameter name first using a double reverse string
       // to get the begining of the string in the array then reverse it again
       // to set it back. we will also "trim" off the "=" sign

    
$param = rtrim(strrev(stristr(strrev($value),'=')),"=");

      
//now lets get what the parameter is set to.
       // again "trimming" off the = sign

    
$setting = ltrim(stristr($value,'='),"=");

      
// now do something with our results.
       // let's just echo them out so we can see that everything is working

     
echo "Array index is ".$index." and value is ".$value."\r\n";
      echo
"Parameter is ".$param." and is set to ".$setting."\r\n\r\n";

}

?>

when run from the CLI this script returns the following.

[root@fedora4 ~]# php a.php -val1=one -val2=two -val3=three

Array index is 0 and value is a.php
Parameter is  and is set to

Array index is 1 and value is val1=one
Parameter is val1 and is set to one

Array index is 2 and value is val2=two
Parameter is val2 and is set to two

Array index is 3 and value is val3=three
Parameter is val3 and is set to three

[root@fedora4 ~]#
spam at php-universe dot com
07-Aug-2005 08:07
Regarding triadsebas at triads dot buildtolearn dot net entry for validating emails, etc.

To use these functions, you need to replace strstri() with stristr() as strstri does not exist.
trojjer
30-Jul-2005 09:51
Regarding previous entry about validation:

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

You have to use strpos('haystack','needle')!==false though, of course, incase it returns zero as the actual position, like it says on that page.
triadsebas at triads dot buildtolearn dot net
21-Jul-2005 09:39
You can use strstr() or strstri() to validate data!
Check this out:
<?php
function validate_email($input) {
if (!
strstri($input, '@')) {
return
false;
}
return
true;
}

function
validate_url($input) {
if (!
strstri($input, 'http://')) {
return
false;
}
return
true;
}
?>
Simple example:
<?php
if (!validate_email($_POST['email'])) {
print
'You did not enter a valid email adress';
}
if (!
validate_url($_POST['url'])) {
print
'You did not enter a valid url.';
}
?>
notepad at codewalkers dot com
05-Jun-2005 05:02
<?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

?>
stoyan at mail dot ebpw dot net
18-Mar-2005 10: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
02-Nov-2003 03: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 07: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
13-Nov-2002 05: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
03-Oct-2002 01: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
30-Aug-2002 10: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
05-Aug-2002 03: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;
}

strlen" width="11" height="7"/> <stripslashes
Last updated: Thu, 31 May 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites