strrpos() : Position of last occurence of the search string

$position=strrpos("Welcome to plus2et.com",'m');
echo $position; // Optput is 21
strrpos() Position of search string
Syntax
$int_position=strrpos(Main_string, Search_string,[optional: offset =0 ]);
Main_string : The string inside of which the search_string will be searched.
search_strig : This is searched within the Main_string.
offset : (optional, integer ) Starting point of search but position count is returned from beginning or ending of the string. Negative integer is allowed to search from end of the string in PHP version 7.0.1

Using Offset

Note that first position is 0 ( zero ) .
Position of last occurrence is returned starting from beginning of the string . This counting of position is irrespective of given offset. If position is given negative then search will start by leaving that many characters from end of the string in backward direction.
echo strrpos("Welcome to plus2et.com",'e'); // Output is 16
echo strrpos("Welcome to plus2et.com",'e', 3); // Output is 16
echo strrpos("Welcome to plus2et.com",'c',-1); // Output is 19
echo strrpos("Welcome to plus2et.com",'c',-4); // Output is 3 
echo strrpos("Welcome to plus2et.com",'o',-1); // Output is 20
echo strrpos("Welcome to plus2et.com",'o',-4); // Output is 9
echo strrpos("Welcome to plus2et.com",'e',-4); // Output is 16 
echo strrpos("Welcome to plus2et.com",'e',-7); // Output is 6 

Checking return value

We may get Boolean value as false and it may also return non-Boolean value. We need to evaluate the return value using === operator or !== to get the correct output. Check this code below and the outputs
$position=strrpos("Welcome",'z'); 
if($position==false){
echo " Not found ";	
}else{
echo " Found and position = ".$position;	
}
// Output is Not found
Using the same code we will search for string W
$position=strrpos("Welcome",'W'); 
if($position==false){
echo " Not found ";	
}else{
echo " Found and position = ".$position;	
}
// Output is Not found
The search string is already present at position 0 . Above code need to change like this to get correct output.
$position=strrpos("Welcome",'W'); 
if($position===false){
echo " Not found ";	
}else{
echo " Found and position = ".$position;	
} // Output is Found and position = 0
Use === or !== to test the return value

case-sensitive

strrpos() is case sensitive
$position=strrpos("Welcome",'M'); 
if($position===false){
echo " Not found ";	
}else{
echo " Found and position = ".$position;	
} // Output is Not Found
  • strpos() case-sensitive search returns position of first occurrence
  • stripos() case-insensitive search returns position of first occurrence
  • strripos() case-insensitive search returns position of last occurrence

String Functions stristr(): String search str_replace(): String replace
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2023 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer