stristr 


string apg

FIND the first occurrence of a STRING.





This function is case-insensitive

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

?>

$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 stristr($strmail'@');

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

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

echo 
$nik;

?>

 RESULT   

@hostname.com

nick


  2 EXERCISE   

<?php

$st 
'Libertas quæ sera tamen';

$dt stristr($st'Q');

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

$at stristr($st'Q'true);

echo 
$at;

?>

 RESULT   

quæ sera tamen

Libertas


  3 EXERCISE   

<?php

$st 
'Libertas quæ sera tamen';

$dt stristr($st'Æ');

var_dump($dt);

echo 
'<br><br>';

$at stristr($st'Æ'true);

var_dump($at);

?>

 RESULT   

bool(false)

bool(false)


  4 EXERCISE   

<?php

$st 
'Libertas quæ sera tamen';

$dt stristr($st'l');

var_dump($dt);

echo 
'<br><br>';

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

var_dump($at);

?>

 RESULT   

string(24) "Libertas quæ sera tamen"

string(0) ""


  5 EXERCISE   

<?php

$st 
'Libertas quæ sera tamen';

$dt stristr($st'N');

var_dump($dt);

echo 
'<br><br>';

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

var_dump($at);

?>

  6 EXERCISE   

<?php

$st 
'Libertas quæ sera tamen';

$dt stristr($st'tAMEN');

var_dump($dt);

echo 
'<br><br>';

$at stristr($st'tAMEN'true);

var_dump($at);

?>

  7 EXERCISE   

<?php

$st 
'Libertas quæ sera tamen';

$dt stristr($stNULL);

var_dump($dt);

echo 
'<br><br>';

$at stristr($stNULLtrue);

var_dump($at);

?>

  8 EXERCISE   

<?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$srctrue);

var_dump($at);

?>

  9 EXERCISE   

<?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($email97));
echo 
"<br>e-mail: $email<br>";
var_dump(stristr($email971));

?>

  10 EXERCISE   

<?php

echo "Testing stristr() : basic functionality.<br><br>";

var_dumpstristr("Test string""teSt") );
echo 
'<br><br>';
var_dumpstristr("test stRIng""striNG") );
echo 
'<br><br>';
var_dumpstristr("teST StrinG""stRIn") );
echo 
'<br><br>';
var_dumpstristr("tesT string""t S") );
echo 
'<br><br>';
var_dumpstristr("test strinG""g") );
echo 
'<br><br>';
var_dumpbin2hex(stristr("te".chr(0)."St"chr(0))) );
echo 
'<br><br>';
var_dumpstristr("tEst""test") );
echo 
'<br><br>';
var_dumpstristr("teSt""test") );
echo 
'<br><br><br>';

var_dumpstristr("Test String""String"false) );
echo 
'<br><br>';
var_dumpstristr("Test String""String"true) );

?>

  11 EXERCISE   

<?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(
12),

          
// 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_dumpstristr("Hello World"$input) );
  } catch (
TypeError $e) {
    echo 
$e->getMessage(), "<br>";
  }
  
$count ++;
}

fclose($file_handle);  
//closing the file handle

?>

  12 EXERCISE   

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

?>