strripos – PHP String Functions

Syntax :

strripos ( string, search_string, start );

Description :

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

Note: The function is case-insensitive.

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 : substr()strrchr() , strrpos() , stripos(), strpbrk() , strpos(), stristr().


strripos() – PHP Functions Example 1 :
<?php
$mystring = 'tutor';
$findme = 'T';
$pos = strripos($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.

strripos() – 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 = strripos($newstring, 'H', 0); 
echo $pos;
?>

Output of above code in the browser is as below:

18

strripos() – 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 = strripos($newstring, 'b', 1); 
var_dump($pos);
?>

Output of above code is :

bool(false)

You may also like...