strrchr 


string apg

FIND the LAST occurrence of a character 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.

If $needle contains more than one character, only the first is used.

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 strrchr 
str $haystack mix $needle 


where,

$haystack The input STRING

$needle 
The needle to be found

?>

$haystack


The input STRING.



$needle


The character to be found.



  1 EXERCISE   

<?php 

$strmail 
'nick@hostname.com'

$dom strrchr($strmail'@'); 

echo 
$dom '<br><br>'

$nik stristr($strmail'HOSTNAME'); 

echo 
$nik

?>

 RESULT   

@hostname.com

hostname.com


  2 EXERCISE   

<?php 

$path 
'/' .'blahblahblah.txt'

$dom strrchr($path'/' ); 

echo 
$dom '<br><br>'

?>

 RESULT   

/blahblahblah.txt

  3 EXERCISE   

<?php

var_dump
(strrchr(""""));
echo 
'<br><br>';
var_dump(strrchr("abc"""));
echo 
'<br><br>';
var_dump(strrchr("""abc"));
echo 
'<br><br>';
var_dump(strrchr("abc""abc"));
echo 
'<br><br>';
var_dump(strrchr("test ".chr(0)." test"" "));
echo 
'<br><br>';
var_dump(strrchr("test".chr(0)."string""t"));

?>

  4 EXERCISE   

<?php

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

var_dumpstrrchr("Hello, World""H") ); 
// needle as single char
echo '<br><br>';
var_dumpstrrchr("Hello, World""Hello") ); 
// needle as a first word of haystack
echo '<br><br>';
var_dumpstrrchr('Hello, World''H') );
echo 
'<br><br>';
var_dumpstrrchr('Hello, World''Hello') );
echo 
'<br><br><br>';
// considering case
var_dumpstrrchr("Hello, World""h") );
echo 
'<br><br>';
var_dumpstrrchr("Hello, World""hello") );
echo 
'<br><br><br>';
// needle as second word of haystack
var_dumpstrrchr("Hello, World""World") );
echo 
'<br><br>';
var_dumpstrrchr('Hello, World''World') );
echo 
'<br><br><br>';
// needle as special char
var_dumpstrrchr("Hello, World"",") );
echo 
'<br><br>';
var_dumpstrrchr('Hello, World'',') );
echo 
'<br><br><br>';
var_dumpstrrchr("Hello, World""Hello, World") ); 
// needle as haystack
echo '<br><br><br>';
// needle string containing one 
// existing and one non-existing char
var_dumpstrrchr("Hello, World""Hi") );

//multiple existence of needle in haystack
var_dumpstrrchr("Hello, World""o") );
echo 
'<br><br>';
var_dumpstrrchr("Hello, World""ooo") );
echo 
'<br><br><br>';
var_dumpstrrchr("Hello, World""Zzzz") );
// non-existent needle in haystack

?>

  5 EXERCISE   

<?php

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

echo "Testing strrchr() function: 
                with various double quoted strings:<br><br>"
;
                
$haystack "Hello,\t\n\0\n  $&!#%\o,()*+-./:;
<=>?@hello123456he \x234 \101 "
;

$needle = [
  
//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
  
" ",
  
"$",
  
" $",
  
"&",
  
"!#",
  
"%\o",
  
"\o,",
  
"()",
  
"*+",
  
"+",
  
"-",
  
".",
  
".;",
  
":;",
  
";",
  
"<=>",
  
">",
  
"=>",
  
"?",
  
"@",
  
"@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
  
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_dumpstrrchr($haystack$needle[$index]) );
  
$count++;
}

?>

  6 EXERCISE   

<?php

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

echo "Testing strrchr() function: 
                    with various single quoted strings:<br><br>"
;
                    
$haystack 'Hello,\t\n\0\n  $&!#%\o,()*+-./:;
<=>?@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
  
' ',
  
'$',
  
' $',
  
'&',
  
'!#',
  
'%\o',
  
'\o,',
  
'()',
  
'*+',
  
'+',
  
'-',
  
'.',
  
'.;',
  
':;',
  
';',
  
'<=>',
  
'>',
  
'=>',
  
'?',
  
'@',
  
'@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_dumpstrrchr($haystack$needle[$index]) );
  
$count ++;
}

?>

  7 EXERCISE   

<?php

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

echo "Testing strrchr() function: 
                     with heredoc strings:<br><br>"
;

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

$needles = array(
  
"ing",
  
"",
  
" ",
  
$multi_line_str
  
//needle as haystack
);

//loop through to test strrchr() with each needle
foreach($needles as $needle) {
  
var_dumpstrrchr($multi_line_str$needle) );
  echo 
'<br><br>';
}

?>

  8 EXERCISE   

<?php

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

echo "Testing strrchr() function: 
                 with heredoc strings:<br><br>"
;
                 
$special_chars_str = <<<EOD
Example of heredoc string contains
$#%^*&*_("_")!#@@!$#$^^&*(special)
chars.
EOD;

$heredoc_needle = <<<EOD
^^&*(
EOD;

$needles = [
  
"!@@!",
  
'_',
  
'("_")',
  
"$*",
  
"(special)",
  
$heredoc_needle,  
  
//needle as heredoc string
  
$special_chars_str  
  
//needle as haystack
];

//loop through to test strrchr() with each needle
foreach($needles as $needle) {
  
var_dumpstrrchr($special_chars_str$needle) );
  echo 
'<br><br>';
}

?>

  9 EXERCISE   

<?php

/* Prototype  : 
 * string strrchr(string $haystack, string $needle);
 * Description: 
 * Finds the last occurrence of a character in a string.
 * Source code: ext/standard/string.c
*/

/* Test strrchr() function by 
 * passing heredoc string containing
 *  escape sequences for haystack and 
 * with various needles
*/

echo "Testing strrchr() function: 
                   with heredoc strings:<br><br>"
;

$escape_char_str = <<<EOD
\tes\t st\r\rch\r using
\escape \\seque\nce
EOD;

$needles = [
  
"\t",
  
'\n',
  
"\r",
  
"\\",
  
$escape_char_str //needle as haystack
];

//loop through to test strrchr() with each needle
foreach($needles as $needle) {
  
var_dumpstrrchr($escape_char_str$needle) );
  echo 
'<br><br>';
}

?>

  10 EXERCISE   

<?php

/* Test strrchr() function by passing 
 * heredoc string containing quote chars 
 * for haystack and with various needles
*/

echo "Testing strrchr() function: 
                with heredoc strings:<br><br>"
;

$quote_char_str = <<<EOD
"things" "in" "double" "quote"
'things' 'in' 'single' 'quote'
EOD;

$needles = array(
  
"things",
  
"\"things\"",
  
"\'things\'",
  
"in",
  
"quote",
  
$quote_char_str 
  
//needle as haystack
);

//loop through to test strrchr() with each needle
foreach($needles as $needle) {
  
var_dumpstrrchr($quote_char_str$needle) );
  echo 
'<br><br>';
}

?>

  11 EXERCISE   

<?php

/* Test strrchr() function by passing 
 * heredoc string containing
 * blank-line for haystack and 
 * with various needles
*/

echo "Testing strrchr() function: with heredoc strings:<br><br>";
$blank_line = <<<EOD

EOD;

$needles = array(
  
"\n",
  
'\n',
  
"\r",
  
"\r\n",
  
"\t",
  
"",
  
$blank_line 
  
//needle as haystack
);

//loop through to test strrchr() with each needle
foreach($needles as $needle) {
  
var_dumpstrrchr($blank_line$needle) );
  echo 
'<br><br>';
}

?>

  12 EXERCISE   

<?php

/* Test strrchr() function by passing 
 * empty heredoc string for haystack
 * and with various needles
*/

echo "Testing strrchr() function: with heredoc strings:<br><br>";

$empty_str = <<<EOD
EOD;

$needles = array(
  
"",
  
'',
  
FALSE,
  
NULL,
  
"\0",
  
$empty_str 
  
//needle as haystack
);

//loop through to test strrchr() with each needle
foreach($needles as $needle) {
  
var_dumpstrrchr($empty_str$needle) );
  echo 
'<br><br>';
}

?>