strrpos – PHP String Functions

Syntax :

strrpos ( string, search_string, start );

Description :

It’s an inbuilt function of PHP. strrpos() function  Find the position of the last occurrence of a search_string in a string.

Note: The function is case-sensitive.

Parameter :

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|left|left|left”]
Name, Required /Optional, Value Type, Description

string, Required, String, Main string to check into.

search_string, Required, String, String to be searched for.

start, OptionalString,  Specifies where to start searching in string.

[/table]


Output :

Returns the of the last occurrence of a search_string inside string. Also note that string positions start at 0, and not 1.

Returns FALSE if the search_string was not found.


ChangeLog :

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
PHP Version, Description
5.0.0, The search_string may now be a string of more than one character.

[/table]


Related articles : stripos(), strpos(), strripos(), strrchr()substr().


strrpos() – PHP Functions Example 1 :
<?php
$mystring = 'tutor';
$findme = 't';
$pos = strrpos($mystring, $findme);

if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
?>

Output of above code in the browser is as below:

The string ‘t’ was found in the string ‘tutor’ and exists at position 2.

strrpos() – PHP Functions Example 2 : Using length parameter.
<?php
// We can search for the character, ignoring anything before the offset
$newstring = 'hello from tutor. hi all.';
$pos = strrpos($newstring, 'h', 0); 
echo $pos;
?>

Output of above code in the browser is as below:

18

strrpos() – PHP Functions Example 3 :   Returns FALSE if the search_string was not found.
<?php
// We can search for the character, ignoring anything before the offset
$newstring = 'hello from tutor. hi all.';
$pos = strrpos($newstring, 'b', 1); 
var_dump($pos);
?>

Output of above code is :

bool(false)

You may also like...