strrpos
FIND the position of the LAST occurrence of a of a substring in a STRING.
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.
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 strrpos ( 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.
EXERCISE
<?php
$str_hp = 'Estou procurando uma agulha
em um palheiro! (UM TRABALHO DIFÍCIL)';
$str_np = 'um';
$int_ap = strrpos($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 = strrpos($str_he, $str_ne);
echo 'The LAST occurrence of [' . $str_ne . ']
<br><br> in, "' . $str_he . '"
<br><br> is found at position [' . $int_ae . ']';
?>
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 = strrpos($str_hpx2, $str_np2, $off_o2);
$int_ap22 = strrpos($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;
?>
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 = strrpos($str_hpx3, $str_np3, $off_o3);
$int_ap32 = strrpos($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;
?>
EXERCISE
<?php
$str_hpx4 = "I'm looking for a needle
in a haystack! (A HARD WORK)";
$str_np4 = 'haystack';
$off_p4 = -50;
$int_ap42 = strrpos($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>';
}
?>
EXERCISE
<?php
$str_hpx5 = "I'm looking for a needle
in a haystack! (A HARD WORK)";
$str_np5 = 'HAYSTACK';
$off_p5 = -50;
$int_ap52 = strrpos($str_hpx5, $str_np5, $off_p5);
echo $str_hpx5;
if($int_ap52 === TRUE)
{
echo '<br><br><br>The first occurrence of ' . $str_np5
. ', is found at ' . $int_ap52 . ',
in 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>';
}
?>
EXERCISE
<?php
var_dump(strrpos("test test string", "test"));
echo '<br><br>';
var_dump(strrpos("test string sTring", "string"));
echo '<br><br>';
var_dump(strrpos("test strip string strand", "str"));
echo '<br><br>';
var_dump(strrpos("I am what I am and that's all what I am",
"am", -3));
echo '<br><br>';
var_dump(strrpos("test string", "g"));
echo '<br><br>';
var_dump(strrpos("te".chr(0)."st", chr(0)));
echo '<br><br>';
var_dump(strrpos("tEst", "test"));
echo '<br><br>';
var_dump(strrpos("teSt", "test"));
echo '<br><br>';
var_dump(strrpos("foo", "f", 1));
echo '<br><br>';
var_dump(strrpos("", ""));
echo '<br><br>';
var_dump(strrpos("a", ""));
echo '<br><br>';
var_dump(strrpos("", "a"));
echo '<br><br>';
var_dump(strrpos("\\\\a", "\\a"));
?>
EXERCISE
<?php
echo "Testing strrpos() function:
basic functionality.<br>";
$heredoc_str = <<<EOD
Hello, World
EOD;
echo "<br><br>With default arguments:<br>";
//regular string for haystack & needle
var_dump( strrpos("Hello, World", "Hello") );
echo '<br><br>';
var_dump( strrpos('Hello, World', "hello") );
echo '<br><br>';
var_dump( strrpos("Hello, World", 'World') );
echo '<br><br>';
var_dump( strrpos('Hello, World', 'WORLD') );
echo '<br><br><br>';
//single char for needle
var_dump( strrpos("Hello, World", "o") );
echo '<br><br>';
var_dump( strrpos("Hello, World", ",") );
echo '<br><br><br>';
//heredoc string for haystack & needle
var_dump( strrpos($heredoc_str, "Hello, World") );
echo '<br><br>';
var_dump( strrpos($heredoc_str, 'Hello') );
echo '<br><br>';
var_dump( strrpos($heredoc_str, $heredoc_str) );
?>
EXERCISE
<?php
echo "Testing strrpos() function: basic functionality.<br>";
$heredoc_str = <<<EOD
Hello, World
EOD;
echo "<br><br>With all arguments;<br>";
//regular string for haystack & needle, with various offsets
var_dump( strrpos("Hello, World", "Hello", 0) );
echo '<br><br>';
var_dump( strrpos("Hello, World", 'Hello', 1) );
echo '<br><br>';
var_dump( strrpos('Hello, World', 'World', 1) );
echo '<br><br>';
var_dump( strrpos('Hello, World', "World", 5) );
echo '<br><br><br>';
//heredoc string for haystack & needle, with various offsets
var_dump( strrpos($heredoc_str, "Hello, World", 0) );
echo '<br><br>';
var_dump( strrpos($heredoc_str, 'Hello', 0) );
echo '<br><br>';
var_dump( strrpos($heredoc_str, 'Hello', 1) );
echo '<br><br>';
var_dump( strrpos($heredoc_str, $heredoc_str, 0) );
echo '<br><br>';
var_dump( strrpos($heredoc_str, $heredoc_str, 1) );
echo '<br><br><br>';
//various offsets
var_dump( strrpos("Hello, World", "o", 3) );
echo '<br><br>';
var_dump( strrpos("Hello, World", "o", 6) );
echo '<br><br>';
var_dump( strrpos("Hello, World", "o", 10) );
?>
EXERCISE
<?php
var_dump(strrpos("haysthack", 'ha', -9));
echo '<br><br>';
var_dump(strrpos("haystack", 'h', -8));
echo '<br><br>';
var_dump(strrpos("haystack", 'k', -1));
echo '<br><br>';
var_dump(strrpos("haystack", "ka", -1));
echo '<br><br>';
var_dump(strrpos("haystack", 'a', -3));
echo '<br><br>';
var_dump(strrpos("haystack", 'a', -4));
echo '<br><br>';
try {
strrpos("haystack", 'h', -9);
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
echo '<br><br>';
var_dump(strripos("HAYSTHACk", 'ha', -9));
echo '<br><br>';
var_dump(strripos("HAYSTACK", 'h', -8));
echo '<br><br>';
var_dump(strripos("HAYSTACK", 'k', -1));
echo '<br><br>';
var_dump(strripos("HAYSTACK", "ka", -1));
echo '<br><br>';
var_dump(strripos("HAYSTACK", 'a', -3));
echo '<br><br>';
var_dump(strripos("HAYSTACK", 'a', -4));
echo '<br><br>';
try {
strripos("HAYSTACK", 'h', -9);
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
?>
EXERCISE
<?php
try {
var_dump(strrpos("t", "t", PHP_INT_MAX+1));
} catch (TypeError $e) {
echo $e->getMessage(), "<br>";
}
try {
strrpos(1024, 1024, -PHP_INT_MAX);
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
try {
strrpos(1024, "te", -PHP_INT_MAX);
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
try {
strrpos(1024, 1024, -PHP_INT_MAX-1);
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
try {
strrpos(1024, "te", -PHP_INT_MAX-1);
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
?>
EXERCISE
<?php
/* Test strrpos() function by passing
* double quoted strings for 'haystack'
* & 'needle' arguments */
echo "Testing strrpos() function: with double quoted strings.<br>";
$haystack = "Hello,\t\n\0\n $&!#%()*<=>?@hello123456he \x234 \101 ";
$needle = array(
//regular strings
"l",
"L",
"HELLO",
"hEllo",
//escape characters
"\t",
"\T", //invalid input
" ",
"\n",
"\N", //invalid input
"
", //new line
//nulls
"\0",
NULL,
null,
//boolean false
FALSE,
false,
//empty string
"",
//special chars
" ",
"$",
" $",
"&",
"!#",
"()",
"<=>",
">",
"=>",
"?",
"@",
"@hEllo",
"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;
for($index=0; $index<count($needle); $index++) {
echo "<br><br>Iteration: $count<br>";
var_dump( strrpos($haystack, $needle[$index]) );
echo '<br><br>';
var_dump( strrpos($haystack, $needle[$index], $index) );
$count++;
}
?>
EXERCISE
<?php
/* Test strrpos() function by passing
* single quoted strings to 'haystack'
* & 'needle' arguments */
echo "Testing strrpos() function:
with single quoted strings.<br>";
$haystack = 'Hello,\t\n\0\n $&!#%()*<=>?@hello123456he \x234 \101 ';
$needle = array(
//regular strings
'l',
'L',
'HELLO',
'hEllo',
//escape characters
'\t',
'\T',
' ',
'\n',
'\N',
'
', //new line
//nulls
'\0',
NULL,
null,
//boolean false
FALSE,
false,
//empty string
'',
//special chars
' ',
'$',
' $',
'&',
'!#',
'()',
'<=>',
'>',
'=>',
'?',
'@',
'@hEllo',
'12345', //decimal numeric string
'\x23', //hexadecimal numeric string
'#', //hexadecimal numeric string
'\101', //octal numeric string
'A',
'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;
for($index=0; $index<count($needle); $index++) {
echo "<br><br>Iteration: $count<br>";
var_dump( strrpos($haystack, $needle[$index]) );
echo '<br><br>';
var_dump( strrpos($haystack, $needle[$index], $index) );
$count++;
}
?>
EXERCISE
<?php
/* Test strrpos() function by
* passing multi-line heredoc string
* for haystack and
* with various needles & offsets
*/
echo "Testing strrpos() function: with heredoc strings.<br>";
echo "<br><br>With heredoc string containing multi lines:<br>";
$multi_line_str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
var_dump( strrpos($multi_line_str, "ing", 0) );
echo '<br><br>';
var_dump( strrpos($multi_line_str, "ing", 15) );
echo '<br><br>';
var_dump( strrpos($multi_line_str, "ing", 22) );
echo '<br><br>';
var_dump( strrpos($multi_line_str, "") );
echo '<br><br>';
var_dump( strrpos($multi_line_str, " ") );
?>
EXERCISE
<?php
/* Test strrpos() function by passing
* heredoc string containing
* special chars for haystack
* and with various needles & offsets
*/
echo "Testing strrpos() function:
with heredoc strings.<br>";
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( strrpos($special_chars_str, "Ex'ple", 0) );
echo '<br><br>';
var_dump( strrpos($special_chars_str, "!@@!", 23) );
echo '<br><br>';
var_dump( strrpos($special_chars_str, '_') );
echo '<br><br>';
var_dump( strrpos($special_chars_str, '("_")') );
echo '<br><br>';
var_dump( strrpos($special_chars_str, "$*") );
echo '<br><br>';
var_dump( strrpos($special_chars_str, "$*", 10) );
echo '<br><br>';
var_dump( strrpos($special_chars_str, "(special)") );
?>
EXERCISE
<?php
/* Test strrpos() function by passing
* heredoc string containing escape chars
* for haystack
* and with various needles & offsets
*/
echo "Testing strrpos() function:
with heredoc strings.<br>";
echo "<br><br>With heredoc string containing
escape characters:<br>";
$control_char_str = <<<EOD
Hello, World\n
Hello\tWorld
EOD;
var_dump( strrpos($control_char_str, "\n") );
echo '<br><br>';
var_dump( strrpos($control_char_str, "\t") );
echo '<br><br>';
var_dump( strrpos($control_char_str, "\n", 12) );
echo '<br><br>';
var_dump( strrpos($control_char_str, "\t", 15) );
?>
EXERCISE
<?php
/* Test strrpos() function by passing heredoc
* string containing quotes for haystack
* and with various needles & offsets
*/
echo "Testing strrpos() function:
with heredoc strings.<br>";
echo "With heredoc string containing
quote & slash chars:<br>";
$quote_char_str = <<<EOD
it's bright,but i cann't see it.
"things in double quote"
'things in single quote'
this\line is /with\slashs
EOD;
var_dump( strrpos($quote_char_str, "line") );
echo '<br><br>';
var_dump( strrpos($quote_char_str, 'things') );
echo '<br><br>';
var_dump( strrpos($quote_char_str, 'things', 0) );
echo '<br><br>';
var_dump( strrpos($quote_char_str, "things", 20) );
?>
EXERCISE
<?php
/* Test strrpos() function by passing
* empty heredoc string for haystack
* and with various needles & offsets
*/
echo "Testing strrpos() function: with heredoc strings.<br>";
echo "<br><br>With empty heredoc string:<br>";
$empty_string = <<<EOD
EOD;
var_dump( strrpos($empty_string, "") );
echo '<br><br>';
try {
strrpos($empty_string, "", 1);
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
var_dump( strrpos($empty_string, FALSE) );
echo '<br><br>';
var_dump( strrpos($empty_string, NULL) );
?>
EXERCISE
<?php
/* Test strrpos() function with strings
* containing multiple occurrences of
* 'needle' in the 'haystack'
* and with various needles & offsets
*/
echo "Testing strrpos() function: strings repetitive chars.<br>";
$haystack = "ababababAbaBa";
$needle = "aba";
/* loop through to consider various
offsets in getting the position of
the needle in haystack string */
$count = 1;
for($offset = -1; $offset <= strlen($haystack); $offset++ ) {
echo "<br><br>Iteration: $count<br>";
var_dump( strrpos($haystack, $needle, $offset) );
$count++;
}
?>
EXERCISE
<?php
/* Test strrpos() function with
* unexpected inputs for 'needle' and
* an expected type of input for
* 'haystack' argument
*/
echo "Testing strrpos() function with
unexpected values for needle.<br>";
//get an unset variable
$unset_var = 'string_val';
unset($unset_var);
//defining a class
class sample {
public function __toString() {
return "object";
}
}
//getting the resource
$file_handle = fopen(__FILE__, "r");
$haystack = "string 0 1 2 -2 10.5 -10.5
10.5e10 10.6E-10 .5 array true
false object \"\" null Resource";
// array with different values
$needles = array (
// integer values
0,
1,
12345,
-2345,
// float values
10.5,
-10.5,
10.1234567e10,
10.7654321E-10,
.5,
// array values
array(),
array(0),
array(1),
array(1, 2),
array('color' => 'red', 'item' => 'pen'),
// boolean values
true,
false,
TRUE,
FALSE,
// objects
new sample(),
// empty string
"",
'',
// null values
NULL,
null,
// resource
$file_handle,
// undefined variable
@$undefined_var,
// unset variable
@$unset_var
);
// loop through each element of the
// 'needle' array to check the
// working of strrpos()
$counter = 1;
for($index = 0; $index < count($needles); $index ++) {
echo "<br><br>Iteration: $counter<br>";
try {
var_dump( strrpos($haystack, $needles[$index]) );
} catch (TypeError $e) {
echo $e->getMessage(), "<br>";
}
$counter ++;
}
fclose($file_handle); //closing the file handle
?>
EXERCISE
<?php
/* Test strrpos() function with unexpected
* inputs for 'haystack' and 'needle' arguments */
echo "Testing strrpos() function with
unexpected values for haystack and needle.<br>";
// get an unset variable
$unset_var = 'string_val';
unset($unset_var);
// defining a class
class sample {
public function __toString() {
return "object";
}
}
//getting the resource
$file_handle = fopen(__FILE__, "r");
// array with different values
$values = array (
// integer values
0,
1,
12345,
-2345,
// float values
10.5,
-10.5,
10.5e10,
10.6E-10,
.5,
// array values
array(),
array(0),
array(1),
array(1, 2),
array('color' => 'red', 'item' => 'pen'),
// boolean values
true,
false,
TRUE,
FALSE,
// objects
new sample(),
// empty string
"",
'',
// null values
NULL,
null,
// resource
$file_handle,
// undefined variable
@$undefined_var,
// unset variable
@$unset_var
);
// loop through each element of the array
// and check the working of strrpos()
$counter = 1;
for($index = 0; $index < count($values); $index ++) {
echo "<br><br>Iteration: $counter<br>";
$haystack = $values[$index];
try {
var_dump( strrpos($values[$index], $values[$index]) );
} catch (Error $e) {
echo get_class($e) . ": " . $e->getMessage(), "<br>";
}
try {
var_dump( strrpos($values[$index], $values[$index], 1) );
} catch (Error $e) {
echo get_class($e) . ": " . $e->getMessage(), "<br>";
}
$counter ++;
}
?>
EXERCISE
<?php
/* Test strrpos() function with null
* terminated strings for 'haystack' argument
* in order to check the binary safe
*/
echo "Test strrpos() function: binary safe.<br>";
$haystacks = array(
"Hello".chr(0)."World",
chr(0)."Hello World",
"Hello World".chr(0),
chr(0).chr(0).chr(0),
"Hello\0world",
"\0Hello",
"Hello\0"
);
for($index = 0; $index < count($haystacks); $index++ ) {
var_dump( strrpos($haystacks[$index], "\0") );
echo '<br><br>';
var_dump( strrpos($haystacks[$index], "\0", $index) );
}
?>
EXERCISE
<?php
/* Test strrpos() function with null
* terminated strings for 'needle' argument
* in order to check binary safe
*/
echo "Test strrpos() function: binary safe.<br>";
$haystack = "\0Hello\0World\0";
$needles = array(
"Hello".chr(0)."World",
chr(0)."Hello\0",
chr(0),
"Hello\0world",
"\0Hello\0world\0",
"\0Hello",
"Hello\0"
);
for($index = 0; $index < count($needles); $index++ ) {
var_dump( strrpos($haystack, $needles[$index]) );
echo '<br><br>';
var_dump( strrpos($haystack, $needles[$index], $index) );
}
?>
EXERCISE
<?php
$haystack = "Hello,\t\n\0\n $&!#%()*<=>?@hello123456he \x234 \101 ";
echo $haystack . '<br>';
$var00 = strlen($haystack);
echo "$var00 characteres.<br><br><br>";
$var01 = strrpos($haystack, "", -1);
var_dump($var01);
echo "<br><br>";
$var02 = strrpos($haystack, "", -10);
var_dump($var02);
echo "<br><br>";
$var03 = strrpos($haystack, "", -26);
var_dump($var03);
echo "<br><br>";
$var04 = strrpos($haystack, "", -44);
var_dump($var04);
echo "<br><br>";
?>