<?php
str stristr ( str $haystack , mix $needle [,
bool $before_needle = FALSE ] )
where,
$haystack = The input STRING
$needle = The needle to be found
$before_needle = To control which part
of haystack will be shown
( SEE the below TABLE )
?>
BOOL | MEANING |
FALSE | Will return part of the $haystack AFTER the first occurrence of $needle. The $needle is included in the returned part. |
TRUE | Will return part of the $haystack BEFORE the first occurrence of $needle |
ed48 |
<?php
// CLASSICAL EXAMPLE
$strmail = '[email protected]';
$dom = stristr($strmail, '@');
echo $dom . '<br><br>';
$nik = stristr($strmail, '@', TRUE);
echo $nik;
?>
<?php
$st = 'Libertas quæ sera tamen';
$dt = stristr($st, 'Q');
echo $dt . '<br><br>';
$at = stristr($st, 'Q', true);
echo $at;
?>
<?php
$st = 'Libertas quæ sera tamen';
$dt = stristr($st, 'Æ');
var_dump($dt);
echo '<br><br>';
$at = stristr($st, 'Æ', true);
var_dump($at);
?>
<?php
$st = 'Libertas quæ sera tamen';
$dt = stristr($st, 'l');
var_dump($dt);
echo '<br><br>';
$at = stristr($st, 'L', true);
var_dump($at);
?>
<?php
$st = 'Libertas quæ sera tamen';
$dt = stristr($st, 'N');
var_dump($dt);
echo '<br><br>';
$at = stristr($st, 'n', true);
var_dump($at);
?>
<?php
$st = 'Libertas quæ sera tamen';
$dt = stristr($st, 'tAMEN');
var_dump($dt);
echo '<br><br>';
$at = stristr($st, 'tAMEN', true);
var_dump($at);
?>
<?php
$st = 'Libertas quæ sera tamen';
$dt = stristr($st, NULL);
var_dump($dt);
echo '<br><br>';
$at = stristr($st, NULL, true);
var_dump($at);
?>
<?php
$st = 'M3&]R96T@27!S=6T@:7,@<VEM<&QY
(&$@=&5X="!S:6UU;&%T:6]N(&]F(\'1H
M92!T>7!O9W)A<&AY(&%N9"!P<FEN=&EN9
R!I;F1U<W1R>2!A;F0@:&%S(&)E
M96X@:6X@=7-E(\'-I;F-E(\'1H
92`Q-G1H(&-E;G1U';
$src = '7,@';
$dt = stristr($st, $src);
var_dump($dt);
echo '<br><br>';
$at = stristr($st, $src, true);
var_dump($at);
?>
<?php
$email = 'AbcCdEfGh';
echo "e-mail: $email<br>";
var_dump(stristr($email, 'c'));
echo "<br>e-mail: $email<br>";
var_dump(stristr($email, 'c', 1));
echo '<br><br>';
$email = 'AbCdeEfGh';
echo "e-mail: $email<br><br>";
var_dump(stristr($email, 'E'));
echo "<br>e-mail: $email<br>";
var_dump(stristr($email, 'E', 1));
echo '<br><br>';
$email = 'wazAbCdeEfGh';
echo "e-mail: $email<br>";
var_dump(stristr($email, 97));
echo "<br>e-mail: $email<br>";
var_dump(stristr($email, 97, 1));
?>
<?php
echo "Testing stristr() : basic functionality.<br><br>";
var_dump( stristr("Test string", "teSt") );
echo '<br><br>';
var_dump( stristr("test stRIng", "striNG") );
echo '<br><br>';
var_dump( stristr("teST StrinG", "stRIn") );
echo '<br><br>';
var_dump( stristr("tesT string", "t S") );
echo '<br><br>';
var_dump( stristr("test strinG", "g") );
echo '<br><br>';
var_dump( bin2hex(stristr("te".chr(0)."St", chr(0))) );
echo '<br><br>';
var_dump( stristr("tEst", "test") );
echo '<br><br>';
var_dump( stristr("teSt", "test") );
echo '<br><br><br>';
var_dump( stristr("Test String", "String", false) );
echo '<br><br>';
var_dump( stristr("Test String", "String", true) );
?>
<?php
echo "Testing stristr() function:
with unexpected inputs for
'needle' argument.<br>";
//get an unset variable
$unset_var = 'string_val';
unset($unset_var);
//defining a class
class sample {
public function __toString() {
return "sample object";
}
}
//getting the resource
$file_handle = fopen(__FILE__, "r");
// array with different values for $input
$inputs = array (
// integer values
/*1*/ 0,
1,
-2,
-PHP_INT_MAX,
// float values
/*5*/ 10.5,
-20.5,
10.1234567e10,
// array values
/*8*/ array(),
array(0),
array(1, 2),
// boolean values
/*11*/ true,
false,
TRUE,
FALSE,
// null values
/*15*/ NULL,
null,
// objects
/*17*/ new sample(),
// resource
/*18*/ $file_handle,
// undefined variable
/*19*/ @$undefined_var,
// unset variable
/*20*/ @$unset_var
);
//defining '$pad_length' argument
$pad_length = "20";
// loop through with each element of the
// $inputs array to test stristr() function
$count = 1;
foreach($inputs as $input) {
echo "<br><br>Iteration: $count<br>";
try {
var_dump( stristr("Hello World", $input) );
} catch (TypeError $e) {
echo $e->getMessage(), "<br>";
}
$count ++;
}
fclose($file_handle);
//closing the file handle
?>
<?php
var_dump(stristr("tEsT sTrInG", "tEsT"));
echo '<br><br>';
var_dump(stristr("tEsT sTrInG", "stRiNg"));
echo '<br><br>';
var_dump(stristr("tEsT sTrInG", "stRiN"));
echo '<br><br>';
var_dump(stristr("tEsT sTrInG", "t S"));
echo '<br><br>';
var_dump(stristr("tEsT sTrInG", "g"));
echo '<br><br>';
var_dump(md5(stristr("te".chr(0)."st", chr(0))));
echo '<br><br>';
var_dump(stristr("", ""));
echo '<br><br>';
var_dump(stristr("a", ""));
echo '<br><br>';
var_dump(stristr("", "a"));
echo '<br><br>';
var_dump(md5(stristr("\\\\a\\", "\\a")));
echo '<br><br>';
var_dump(stristr("tEsT sTrInG", " "));
?>