<?php
str strstr ( 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 = strstr($strmail, '@');
echo $dom . '<br><br>';
$nik = strstr($strmail, '@', TRUE);
echo $nik;
?>
<?php
$st = 'Libertas quæ sera tamen';
$dt = strstr($st, 'q');
echo $dt . '<br><br>';
$at = strstr($st, 'q', true);
echo $at;
?>
<?php
$st = 'Libertas quæ sera tamen';
$dt = strstr($st, 'æ');
var_dump($dt);
echo '<br><br>';
$at = strstr($st, 'æ', true);
var_dump($at);
?>
<?php
$st = 'Libertas quæ sera tamen';
$dt = strstr($st, 'L');
var_dump($dt);
echo '<br><br>';
$at = strstr($st, 'L', true);
var_dump($at);
?>
<?php
$st = 'Libertas quæ sera tamen';
$dt = strstr($st, 'n');
var_dump($dt);
echo '<br><br>';
$at = strstr($st, 'n', true);
var_dump($at);
?>
<?php
$st = 'Libertas quæ sera tamen';
$dt = strstr($st, 'A');
var_dump($dt);
echo '<br><br>';
$at = strstr($st, 'A', true);
var_dump($at);
?>
<?php
echo "Testing basic functionality of strstr():<br>";
var_dump( strstr("test string", "test") );
echo '<br><br>';
var_dump( strstr("test string", "string") );
echo '<br><br>';
var_dump( strstr("test string", "strin") );
echo '<br><br>';
var_dump( strstr("test string", "t s") );
echo '<br><br>';
var_dump( strstr("test string", "g") );
echo '<br><br>';
var_dump( md5(strstr("te".chr(0)."st", chr(0))) );
echo '<br><br>';
var_dump( strstr("tEst", "test") );
echo '<br><br>';
var_dump( strstr("teSt", "test") );
echo '<br><br>';
var_dump( strstr("", "") );
echo '<br><br>';
var_dump( strstr("a", "") );
echo '<br><br>';
var_dump( strstr("", "a") );
echo '<br><br>';
echo "<br><br>Testing strstr() with various needles:";
$string =
"Hello world,012033 -3.3445
NULL TRUE FALSE\0 abcd\xxyz \x000 octal\n
abcd$:Hello world";
/* needles in an array to get the
string starts with needle, in $string */
$needles = array(
"Hello world",
"WORLD",
"\0",
"\x00",
"\x000",
"abcd",
"xyz",
"octal",
"-3",
-3,
"-3.344",
-3.344,
NULL,
"NULL",
"0",
0,
TRUE,
"TRUE",
"1",
1,
FALSE,
"FALSE",
" ",
" ",
'b',
'\n',
"\n",
"12",
"12twelve",
$string
);
/* loop through to get the string
starts with "needle" in $string */
for( $i = 0; $i < count($needles); $i++ ) {
echo "<br><br>Iteration: $i<br>";
var_dump( strstr($string, $needles[$i]) );
}
echo "<br><br>Testing miscellaneous input data.<br>";
echo "<br>Passing objects as string and needle:<br>";
/* we get "Recoverable fatal error:
* saying Object of class needle could not be
* converted to string" by default when an object
* is passed instead of string:
* The error can be avoided by choosing the __toString
* magix method as follows: */
class StringCapable
{
function __toString() {
return "Hello, world";
}
}
$obj_string = new StringCapable;
class needle
{
function __toString() {
return "world";
}
}
$obj_needle = new needle;
var_dump(strstr("$obj_string", "$obj_needle"));
echo "<br><br>Posiibilities with null:<br>";
var_dump( strstr("", NULL) );
echo '<br><br>';
var_dump( strstr(NULL, NULL) );
echo '<br><br>';
var_dump( strstr("a", NULL) );
echo '<br><br>';
var_dump( strstr("/x0", "0") );
// Hexadecimal NUL
echo '<br><br>';
echo "<br><br>A longer and heredoc string:<br>";
$string = <<<EOD
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
EOD;
var_dump( strstr($string, "abcd") );
echo '<br><br>';
var_dump( strstr($string, "1234") );
echo '<br><br>';
echo "<br><br>A heredoc null string:<br>";
$str = <<<EOD
EOD;
var_dump( strstr($str, "\0") );
echo '<br><br>';
var_dump( strstr($str, NULL) );
echo '<br><br>';
var_dump( strstr($str, "0") );
echo '<br><br>';
echo "<br><br>simple and complex syntax strings:<br>";
$needle = 'world';
/* Simple syntax */
var_dump( strstr("Hello, world", "$needle") );
// works
echo '<br><br>';
var_dump( strstr("Hello, world'S", "$needle'S") );
// works
echo '<br><br>';
var_dump( strstr("Hello, worldS", "$needleS") );
// won't work
echo '<br><br>';
/* String with curly braces, complex syntax */
var_dump( strstr("Hello, worldS", "${needle}S") );
// works
echo '<br><br>';
var_dump( strstr("Hello, worldS", "{$needle}S") );
// works
echo '<br><br>';
echo "<br><br>Testing error conditions:<br>";
var_dump( strstr($string, ""));
echo '<br><br>';
var_dump( strstr("a", "b", "c") );
// args > expected
echo '<br><br>';
var_dump( strstr(NULL, "") );
?>
<?php
$email = 'aexample.com';
echo 'Given e-mail: \'' . $email . '\'<br>';
var_dump(strstr($email, '@'));
echo '<br><br>';
var_dump(strstr($email, '@', 1));
echo '<br><br><br>';
$email = '[email protected]';
echo 'Given e-mail: \'' . $email . '\'<br>';
var_dump(strstr($email, '@'));
echo '<br><br>';
var_dump(strstr($email, '@', 1));
echo '<br><br><br>';
$email = 'asdfasdfas@e';
echo 'Given e-mail: \'' . $email . '\'<br>';
var_dump(strstr($email, '@'));
echo '<br><br>';
var_dump(strstr($email, '@', 1));
echo '<br><br><br>';
$email = '@';
echo 'Given e-mail: \'' . $email . '\'<br>';
var_dump(strstr($email, '@'));
echo '<br><br>';
var_dump(strstr($email, '@', 1));
echo '<br><br><br>';
$email = 'eE@fF';
echo 'Given e-mail: \'' . $email . '\'<br>';
var_dump(strstr($email, 'e'));
echo '<br><br>';
var_dump(strstr($email, 'e', 1));
echo '<br><br>';
var_dump(strstr($email, 'E'));
echo '<br><br>';
var_dump(strstr($email, 'E', 1));
echo '<br><br><br>';
$email = '';
echo 'Given e-mail: \'' . $email . '\'<br>';
var_dump(strstr($email, ' ', ''));
?>
<?php
$string = chr(0).chr(128).chr(129).
chr(234).chr(235).chr(254).chr(255);
$stringAsHex = bin2hex($string);
echo "Positions of some chars in
the string '$stringAsHex'
are as follows:<br><br>";
echo bin2hex( chr(128) ) ." => ";
var_dump( bin2hex( strstr($string, chr(128) ) ) );
echo '<br><br>';
echo bin2hex( chr(255) ) ." => ";
var_dump( bin2hex( strstr($string, chr(255) ) ) );
echo '<br><br>';
echo bin2hex( chr(256) ) ." => ";
var_dump( bin2hex( strstr($string, chr(256) ) ) );
?>