strripos 


string apg

FIND the position of the LAST occurrence of a case-insensitive substring in 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.

If $offset < 0 the search is made from right to left skipping the last $offset bytes of the $haystack and searching the first occurrence of $needle.

If $offset > 0 or $offset = 0 the search is made from left to right skipping the first $offset bytes of the $haystack.

Remember that STRING positions start at 0, and not 1.

Watch...
This function may return FALSE, but may also return a non-Boolean value which evaluates to FALSE.

Use the === operator for testing the return value of this function.



<?php

int strripos 
( str $haystack , mix $needle [, int $offset = 0 ] )


where,

$haystack = The input STRING to search in

$needle 
= The needle to be found

$offset 
= The position where the search will start

?>

$haystack


The STRING to search in.



$needle


The parameter to be found.



$offset


The position where the search begin.



  1 EXERCISE   

<?php

$str_hp 
= 'Estou procurando uma agulha 
em um palheiro! (UM TRABALHO DIFÍCIL)'
;

$str_np = 'um';

$int_ap = strripos($str_hp, $str_np);

echo 
'A ÚLTIMA ocorrência de [' . $str_np . ']
<br><br> em, "' 
. $str_hp . '"
<br><br> é verificada na posição [' 
. $int_ap . ']';

echo 
'<br><br><br><br>';

$str_he = "I'm looking for a needle 
in a haystack! (A HARD WORK)"
;

$str_ne = 'a';

$int_ae = strripos($str_he, $str_ne);

echo 
'The LAST occurrence of [' . $str_ne . ']
<br><br> in, "' 
. $str_he . '"
<br><br> is found at position [' 
. $int_ae  . ']';

?> 

 RESULT   

PORTUGUESE

A ÚLTIMA ocorrência de um

em, "Estou procurando uma agulha em um palheiro! (UM TRABALHO DIFÍCIL)"

é verificada na posição 47


ENGLISH

The LAST occurrence of a

in, "I'm looking for a needle in a haystack! (A HARD WORK)"

is found at position 46



  2 EXERCISE   

<?php

$str_hpx2 
= "I'm looking for a needle 
in a haystack! (A HARD WORK)"
;
$str_hpy2 = "I'm looking for a needle 
in a haystack! (A HARD WORK)"
;
$str_hp2 = "I'm looking for a needle 
in a haystack! (A HARD WORK)"
;

$str_np2 = 'a';
$off_p2 = 18;
$off_o2 = 0;
$int_ap21 = strripos($str_hpx2, $str_np2, $off_o2);
$int_ap22 = strripos($str_hpx2, $str_np2, $off_p2);

echo 
$str_hpx2;

echo  
'<br><br><br>The LAST occurrence of ' . $str_np2
 
. ', is found at position ' . $int_ap21 . ', 
 in relation to the position '
 
. $off_o2 . '<br><br>' . $str_hpy2 . '<br><br>';


echo  
'<br><br><br>The LAST occurrence of ' . $str_np2
 
. ', is found at position ' . $int_ap22 . ', after the position '
  
. $off_p2 . '<br><br>' . $str_hp2;

?>

 RESULT   

I'm looking for a needle in a haystack! (A HARD WORK)


The LAST occurrence of a, is found at position 46, in relation to the position 0

I'm looking for a needle in a haystack! (A HARD WORK)



The LAST occurrence of a, is found at position 46, after the position 18

I'm looking for a needle in a haystack! (A HARD WORK)


  3 EXERCISE   

<?php

$str_hpx3 
= "I'm looking for a needle 
in a haystack! (A HARD WORK)"
;
$str_hpy3 = "I'm looking for a needle 
in a haystack! (A HARD WORK)"
;
$str_hp3 = "I'm looking for a needle 
in a haystack! (A HARD WORK)"
;

$str_np3 = 'A';
$off_p3 = 18;
$off_o3 = 0;
$int_ap31 = strripos($str_hpx3, $str_np3, $off_o3);
$int_ap32 = strripos($str_hpx3, $str_np3, $off_p3);

echo 
$str_hpx3;

echo  
'<br><br><br>The LAST occurrence of ' . $str_np3
 
. ', is found at position ' . $int_ap31 . ', 
 in relation to the position '
 
. $off_o3 . '<br><br>' . $str_hpy3 . '<br><br>';


echo  
'<br><br><br>The LAST occurrence of ' . $str_np3
 
. ', is found at position ' . $int_ap32 . ', 
 after the position '
  
. $off_p3 . '<br><br>' . $str_hp3;

?>

 RESULT   

I'm looking for a needle in a haystack! (A HARD WORK)


The LAST occurrence of A, is found at position 46, in relation to the position 0

I'm looking for a needle in a haystack! (A HARD WORK)



The LAST occurrence of A, is found at position 46, after the position 18

I'm looking for a needle in a haystack! (A HARD WORK)


  4 EXERCISE   

<?php

$str_hpx4 
= "I'm looking for a needle 
in a haystack! (A HARD WORK)"
;

$str_np4 = 'haystack';
$off_p4 = -1;

$int_ap42 = strripos($str_hpx4, $str_np4, $off_p4);

echo 
$str_hpx4;

if(
$int_ap42 == true)
{
echo  
'<br><br><br>The LAST occurrence of ' . $str_np4
 
. ', is found at ' . $int_ap42 . ', 
 in relation to the position '
 
. $off_p4 . '<br><br>' . $str_hpx4 . '<br><br>';
}
else
{
echo 
'<br><br>' . $str_np4 . 
' is not present in ' . $str_hpx4 . '<br>';     
}

?>

  5 EXERCISE   

<?php 

$str_hpx5 
= "I'm looking for a needle 
in a haystack! (A HARD WORK)"
; 

$str_np5 = 'HAYSTACK'; 
$off_p5 = -1; 

$int_ap52 = strripos($str_hpx5, $str_np5, $off_p5); 

echo 
$str_hpx5; 

if(
$int_ap52 == true) 
{ 
echo  
'<br><br><br>The LAST occurrence of ' . $str_np5 
 
. ', is found at ' . $int_ap52 . ', i
 n relation to the position ' 
 
. $off_p5 . '<br><br>' . $str_hpx5 . '<br><br>'; 
} 
else 
{ 
echo 
'<br><br>' . $str_np5 . 
' is not present in ' . $str_hpx5 . '<br>';      
} 

?>

  6 EXERCISE   

<?php  

$str_hpx6 
= '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=
&EN9R!I;F1U<W1R>2!A;F0@:&%S(&)E
M96X@:6X@=7-E(\'-I;F-E(\'1H92`Q-G1H(&-E;G1U'
; 

$str_np6 = '7,@';  
$off_p6 = 0;  

$int_ap62 = strripos($str_hpx6, $str_np6, $off_p6);  

echo 
$str_hpx6;  

if(
$int_ap62 == true)  
{  
echo  
'<br><br><br>The LAST occurrence of ' . $str_np6  
 
. ', is found at ' . $int_ap62 . ', 
 in relation to the position '  
 
. $off_p6 . '<br><br>' . $str_hpx6 . '<br><br>';  
}  
else  
{  
echo 
'<br><br>' . $str_np6 . 
' is not present in ' . $str_hpx6 . '<br>';       
}  

?>

  7 EXERCISE   

<?php

var_dump
(strripos("test test string", "test"));
echo 
'<br><br>';
var_dump(strripos("test string sTring", "string"));
echo 
'<br><br>';
var_dump(strripos("test strip string strand", "str"));
echo 
'<br><br>';
var_dump(strripos("I am what I am and that's all what I am", 
                                                                      
"am", -3));
echo 
'<br><br>';
var_dump(strripos("test string", "g"));
echo 
'<br><br>';
var_dump(strripos("te".chr(0)."st", chr(0)));
echo 
'<br><br>';
var_dump(strripos("tEst", "test"));
echo 
'<br><br>';
var_dump(strripos("teSt", "test"));
echo 
'<br><br>';
var_dump(strripos("foo", "f", 1));
echo 
'<br><br>';
var_dump(strripos("", ""));
echo 
'<br><br>';
var_dump(strripos("a", ""));
echo 
'<br><br>';
var_dump(strripos("", "a"));
echo 
'<br><br>';
var_dump(strripos("\\\\a", "\\a"));

?>

  8 EXERCISE   

<?php

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

$heredoc_str = <<<EOD
Hello, World
EOD;

echo 
"<br><br>regular string for haystack & needle:<br>";
var_dump( strripos("Hello, World", "HeLLo") );
echo 
'<br><br>';
var_dump( strripos('Hello, World', "hello") );
echo 
'<br><br>';
var_dump( strripos("Hello, World", 'WoRLd') );
echo 
'<br><br>';
var_dump( strripos('Hello, World', 'WORLD') );
echo 
'<br><br>';
var_dump( strripos('Hello, World', 'foo') );

echo 
"<br><br><br>single char for needle:<br>";
var_dump( strripos("Hello, World", "O") );
echo 
'<br><br>';
var_dump( strripos("Hello, World", ",") );

echo 
"<br><br><br>heredoc string for haystack & needle:<br>";
var_dump( strripos($heredoc_str, "Hello, WoRLd") );
echo 
'<br><br>';
var_dump( strripos($heredoc_str, 'HelLO') );
echo 
'<br><br>';
var_dump( strripos($heredoc_str, $heredoc_str) );

?>

  9 EXERCISE   

<?php

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

$heredoc_str = <<<EOD
Hello, World
EOD;

echo 
"<br><br>regular string for haystack & needle, 
                             <br>with various offsets:<br>"
;
var_dump( strripos("Hello, World", "HeLLo", 0) );
echo 
'<br><br>';
var_dump( strripos("Hello, World", 'Hello', 1) );
echo 
'<br><br>';
var_dump( strripos('Hello, World', 'world', 1) );
echo 
'<br><br>';
var_dump( strripos('Hello, World', "WorLD", 5) );

echo 
"<br><br><br>heredoc string for haystack & needle, 
                             <br>with various offsets:<br>"
;
var_dump( strripos($heredoc_str, "Hello, WORLD", 0) );
echo 
'<br><br>';
var_dump( strripos($heredoc_str, 'HelLo', 0) );
echo 
'<br><br>';
var_dump( strripos($heredoc_str, 'HeLLo', 1) );
echo 
'<br><br>';
var_dump( strripos($heredoc_str, $heredoc_str, 0) );
echo 
'<br><br>';
var_dump( strripos($heredoc_str, $heredoc_str, 1) );

echo 
"<br><br><br>various +ve offsets:<br>";
var_dump( strripos("Hello, World", "O", 3) );
echo 
'<br><br>';
var_dump( strripos("Hello, World", "O", 6) );
echo 
'<br><br>';
var_dump( strripos("Hello, World", "O", 10) );

echo 
"<br><br><br>various -ve offsets:<br>";
var_dump( strripos("Hello, World", "O", -1) );
echo 
'<br><br>';
var_dump( strripos("Hello, World", "O", -5) );
echo 
'<br><br>';
var_dump( strripos("Hello, World", "O",  -9) );

?>

  10 EXERCISE   

<?php

try {
    
var_dump(strripos("t", "t", PHP_INT_MAX+1));
} catch (
TypeError $e) {
    echo 
$e->getMessage(), "<br><br>";
}

try {
    
strripos(1024, 1024, -PHP_INT_MAX);
} catch (
ValueError $exception) {
    echo 
$exception->getMessage() . "<br><br>";
}

try {
    
strripos(1024, "te", -PHP_INT_MAX);
} catch (
ValueError $exception) {
    echo 
$exception->getMessage() . "<br><br>";
}

try {
    
strripos(1024, 1024, -PHP_INT_MAX-1);
} catch (
ValueError $exception) {
    echo 
$exception->getMessage() . "<br><br>";
}

try {
    
strripos(1024, "te", -PHP_INT_MAX-1);
} catch (
ValueError $exception) {
    echo 
$exception->getMessage() . "<br>";
}

?>

  11 EXERCISE   

<?php

/* Test strripos() function by passing 
 * double quoted strings for 'haystack' 
 * & 'needle' arguments */

echo "Testing strripos() function: 
                with double quoted strings."
;

$haystack = "Hello,\t\n\0\n  $&!#%()*<=>?@hello123456he \x234 \101 ";

$needles = array(
          
//regular strings
/*1*/      
"l",
          
"L",
          
"HELLO",
          
"hEllo",

          
//escape characters
/*5*/      
"\t",
          
"\T",  //invalid input
          
"     ",
          
"\n",
          
"\N",  //invalid input
          
"
"
,  //new line

          //nulls
/*11*/      
"\0",
          
NULL,
          
null,

          
//boolean false
/*14*/      
FALSE,
          
false,

          
//empty string
/*16*/      
"",

          
//special chars
/*17*/      
" ",
          
"$",
          
" $",
          
"&",
          
"!#",
          
"()",
          
"<=>",
          
">",
          
"=>",
          
"?",
          
"@",
          
"@hEllo",

/*29*/      "12345", //decimal numeric string
          
"\x23",  //hexadecimal numeric string
          
"#",  //respective ASCII char of \x23
          
"\101",  //octal numeric string
          
"A",  //respective ASCII char of \101
          
"456HEE",  //numerics + chars
          
$haystack  //haystack as needle
);

/* loop through to get the position of 
               the needle in haystack string */
$count = 1;
foreach (
$needles as $needle) {
  echo 
"<br><br><br>Iteration: $count<br>";
  
var_dump( strripos($haystack, $needle) );
  echo 
'<br><br>';
  
var_dump( strripos($haystack, $needle, 1) );
  echo 
'<br><br>';
  
var_dump( strripos($haystack, $needle, 20) );
  echo 
'<br><br>';
  
var_dump( strripos($haystack, $needle, -1) );
  
$count++;
}

?>

  12 EXERCISE   

<?php

/* Test strripos() function by passing 
 * single quoted strings to 'haystack' 
 * & 'needle' arguments */

echo "Testing strripos() function: 
                      with single quoted strings."
;
                      
$haystack = 'Hello,\t\n\0\n  $&!#%()*<=>?@hello123456he \x234 \101 ';

$needles = [
          
//regular strings
/*1*/      
'l',
          
'L',
          
'HELLO',
          
'hEllo',

          
//escape characters
/*5*/      
'\t',
          
'\T',
          
'     ',
          
'\n',
          
'\N',
          
'
        '
,  //new line

          //nulls
/*11*/      
'\0',
          
NULL,
          
null,

          
//boolean false
/*14*/      
FALSE,
          
false,

          
//empty string
/*16*/      
'',

          
//special chars
/*17*/      
' ',
          
'$',
          
' $',
          
'&',
          
'!#',
          
'()',
          
'<=>',
          
'>',
          
'=>',
          
'?',
          
'@',
          
'@hEllo',

/*29*/      '12345',     //decimal numeric string
          
'\x23',    //hexadecimal numeric string
          
'#',      //respective ASCII char of \x23
          
'\101',      //octal numeric string
          
'A',         // respective ASCII char for \101
          
'456HEE', //numerics + chars
          
42,         //needle as int(ASCII value of '*')
          
$haystack  //haystack as needle
];

/* loop through to get the position of 
              the needle in haystack string */
$count = 1;
foreach (
$needles as $needle) {
  echo 
"<br><br><br>Iteration: $count<br>";
  
var_dump( strripos($haystack, $needle) );
  echo 
'<br><br>';
  
var_dump( strripos($haystack, $needle, 1) );
  echo 
'<br><br>';
  
var_dump( strripos($haystack, $needle, 20) );
  echo 
'<br><br>';
  
var_dump( strripos($haystack, $needle, -1) );
  
$count++;
}

?>

  13 EXERCISE   

<?php

/* Test strripos() function by passing 
 * multi-line heredoc string for haystack and
 * with various needles & offsets
*/

echo "Testing strripos() function: with heredoc strings.";

echo 
"<br><br>With heredoc string containing multi lines.<br>";

$multi_line_str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

echo 
"<br><br>Multi line strings with +ve offsets:<br>";
var_dump( strripos($multi_line_str, "iNg", 0) );
echo 
'<br><br>';
var_dump( strripos($multi_line_str, "inG", 15) );
echo 
'<br><br>';
var_dump( strripos($multi_line_str, "Ing", 22) );

echo 
"<br><br><br>Multi line strings with +ve offsets:<br>";
var_dump( strripos($multi_line_str, "Ing", -1) );
echo 
'<br><br>';
var_dump( strripos($multi_line_str, "Ing", -17) );
echo 
'<br><br>';
var_dump( strripos($multi_line_str, "Ing", -50) );

echo 
"<br><br><br>Multi line strings with no offset:<br>";
var_dump( strripos($multi_line_str, "spAn") );
echo 
'<br><br>';
var_dump( strripos($multi_line_str, "IPlE") );
echo 
'<br><br>';
var_dump( strripos($multi_line_str, "") );
echo 
'<br><br>';
var_dump( strripos($multi_line_str, " ") );

?>

  14 EXERCISE   

<?php

/* Test strripos() function by passing 
 * heredoc string containing 
 * special chars for haystack
 * and with various needles & offsets
*/

echo "Testing strripos() function: with heredoc strings.";
echo 
"<br><br>With heredoc string containing special chars:<br>";

$special_chars_str = <<<EOD
Ex'ple of h'doc st'g, contains
$#%^*&*_("_")!#@@!$#$^^&$*(special)
chars.
EOD;

var_dump( strripos($special_chars_str, "Ex'pLE", 0) );
echo 
'<br><br>';
var_dump( strripos($special_chars_str, "!@@!", 23) );
echo 
'<br><br>';
var_dump( strripos($special_chars_str, '_') );
echo 
'<br><br>';
var_dump( strripos($special_chars_str, '("_")') );
echo 
'<br><br>';
var_dump( strripos($special_chars_str, "$*") );
echo 
'<br><br>';
var_dump( strripos($special_chars_str, "$*", 10) );
echo 
'<br><br>';
var_dump( strripos($special_chars_str, "(speCIal)") );

?>

  15 EXERCISE   

<?php

/* Test strripos() function by passing 
 * heredoc string containing 
 * escape chars for haystack
 * and with various needles & offsets
*/

echo "Testing strripos() function: with heredoc strings.";
echo 
"<br><br>With heredoc string containing
                                      escape characters:<br>"
;

$control_char_str = <<<EOD
Hello, World\n
Hello\tWorld
EOD;

var_dump( strripos($control_char_str, "\n") );
echo 
'<br><br>';
var_dump( strripos($control_char_str, "\t") );
echo 
'<br><br>';
var_dump( strripos($control_char_str, "\n", 12) );
echo 
'<br><br>';
var_dump( strripos($control_char_str, "\t", 15) );

?>

  16 EXERCISE   

<?php

$haystack 
= "Hello,\t\n\0\n  $&!#%()*<=>?@hello123456he \x234 \101 ";

echo 
$haystack . '<br><br>';
var_dump(strlen($haystack));
echo 
'<br><br>';
var_dump( strripos($haystack, "", -1) );
echo 
'<br><br>';
var_dump( strripos($haystack, "", -10) );
echo 
'<br><br>';
var_dump( strripos($haystack, "", -26) );
echo 
'<br><br>';
var_dump( strripos($haystack, "", -44) );

?>