strstr 


string apg

FIND the first occurrence of a STRING.

This function is an alias of strchr.



 strchr 


string apg

FIND the first occurrence of a STRING.

This function is an alias of strstr.





This function is case-sensitive

If $needle is not a STRING, it is converted to an INTEGER and applied as the ordinal value of a character.

This behavior is deprecated as of PHP 7.3.0, and relying on it is highly discouraged.

Depending on the intended behavior, the $needle should either be explicitly cast to string, or an explicit call to the function chr should be performed.

This function returns the portion of STRING, or FALSE if $needle is not found.

This function is binary-safe

In PHP, some functions are marked as binary-safe.
It means that the functions works correctly even when you pass binary data.



<?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 )

?>

$haystack


The input STRING.



$needle


The parameter to be found.



$before_needle

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


  1 EXERCISE   

<?php

// CLASSICAL EXAMPLE

$strmail 'nick@hostname.com';

$dom strstr($strmail'@');

echo 
$dom '<br><br>';

$nik strstr($strmail'@'TRUE);

echo 
$nik;

?>

 RESULT   

@hostname.com

nick


  2 EXERCISE   

<?php

$st 
'Libertas quæ sera tamen';

$dt strstr($st'q');

echo 
$dt '<br><br>';

$at strstr($st'q'true);

echo 
$at;

?>

 RESULT   

quæ sera tamen

Libertas


  3 EXERCISE   

<?php

$st 
'Libertas quæ sera tamen';

$dt strstr($st'æ');

var_dump($dt);

echo 
'<br><br>';

$at strstr($st'æ'true);

var_dump($at);

?>

  4 EXERCISE   

<?php

$st 
'Libertas quæ sera tamen';

$dt strstr($st'L');

var_dump($dt);

echo 
'<br><br>';

$at strstr($st'L'true);

var_dump($at);

?>

  5 EXERCISE   

<?php

$st 
'Libertas quæ sera tamen';

$dt strstr($st'n');

var_dump($dt);

echo 
'<br><br>';

$at strstr($st'n'true);

var_dump($at);

?>

  6 EXERCISE   

<?php

$st 
'Libertas quæ sera tamen';

$dt strstr($st'A');

var_dump($dt);

echo 
'<br><br>';

$at strstr($st'A'true);

var_dump($at);

?>

  7 EXERCISE   

<?php
echo "Testing basic functionality of strstr():<br>";
var_dumpstrstr("test string""test") );
echo 
'<br><br>';
var_dumpstrstr("test string""string") );
echo 
'<br><br>';
var_dumpstrstr("test string""strin") );
echo 
'<br><br>';
var_dumpstrstr("test string""t s") );
echo 
'<br><br>';
var_dumpstrstr("test string""g") );
echo 
'<br><br>';
var_dumpmd5(strstr("te".chr(0)."st"chr(0))) );
echo 
'<br><br>';
var_dumpstrstr("tEst""test") );
echo 
'<br><br>';
var_dumpstrstr("teSt""test") );
echo 
'<br><br>';
var_dumpstrstr("""") );
echo 
'<br><br>';
var_dumpstrstr("a""") );
echo 
'<br><br>';
var_dumpstrstr("""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_dumpstrstr($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_dumpstrstr(""NULL) );
echo 
'<br><br>';
var_dumpstrstr(NULLNULL) );
echo 
'<br><br>';
var_dumpstrstr("a"NULL) );
echo 
'<br><br>';
var_dumpstrstr("/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_dumpstrstr($string"abcd") );
echo 
'<br><br>';
var_dumpstrstr($string"1234") );
echo 
'<br><br>';

echo 
"<br><br>A heredoc null string:<br>";
$str = <<<EOD
EOD;
var_dumpstrstr($str"\0") );
echo 
'<br><br>';
var_dumpstrstr($strNULL) );
echo 
'<br><br>';
var_dumpstrstr($str"0") );
echo 
'<br><br>';

echo 
"<br><br>simple and complex syntax strings:<br>";
$needle 'world';

/* Simple syntax */
var_dumpstrstr("Hello, world""$needle") );
// works
echo '<br><br>';
var_dumpstrstr("Hello, world'S""$needle'S") );
// works
echo '<br><br>';
var_dumpstrstr("Hello, worldS""$needleS") );
// won't work
echo '<br><br>';
/* String with curly braces, complex syntax */
var_dumpstrstr("Hello, worldS""${needle}S") );
// works
echo '<br><br>';
var_dumpstrstr("Hello, worldS""{$needle}S") );
// works
echo '<br><br>';
echo 
"<br><br>Testing error conditions:<br>";
var_dumpstrstr($string""));
echo 
'<br><br>';
var_dumpstrstr("a""b""c") );
// args > expected
echo '<br><br>';
var_dumpstrstr(NULL"") );

?>

  8 EXERCISE   

<?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  'a@example.com';
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' '''));

?>

  9 EXERCISE   

<?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 
bin2hexchr(128) ) ." => ";
var_dumpbin2hexstrstr($stringchr(128) ) ) );
echo 
'<br><br>';
echo 
bin2hexchr(255) ) ." => ";
var_dumpbin2hexstrstr($stringchr(255) ) ) );
echo 
'<br><br>';
echo 
bin2hexchr(256) ) ." => ";
var_dumpbin2hexstrstr($stringchr(256) ) ) );

?>