PHP strrpos| Finds the position of the last occurrence of a string?
Summary: In this article, you will learn how to use PHP strrpos to finds the position of the last occurrence of a string inside another string? Let’s understand the PHP strrpos() function with an example.
Definition and Usage PHP strrpos() Function
The PHP strrpos() function used to finds the position of the last occurrence of a string inside another string.
The strrpos() function used to find the numeric position of the last presence of a character in a string. this is a PHP in-build function. this function is case-sensitive, which means this PHP strrpos() function treats uppercase and lowercase characters differently.
The PHP strrpos() function is similar to the PHP strripos() function, which is also used to find the last occurrence of the substring inside another string, but the PHP strripos() function is a case-insensitive function whereas PHP strrpos() is a case-sensitive function.
It is a case-sensitive
There are few related PHP functions that are similar to the strrpos() function:
- stripos() – used to finds the position of the first(1st) occurrence of a string inside another string. It is a Case-insensitive.
- strpos() – Finds the position of the first occurrence of a string inside another string. It is a Case-sensitive.
- strripos() – Finds the position of the last occurrence of a string inside another string. It is a Case-insensitive.
Syntax
The following PHP strrpos() function syntax is:
Parameter
The strrpos() function in PHP accepts three parameters during which two are mandatory, i.e., the main string and the search string. The 3rd parameter is optional, that is $start, from where we start the search of the string.
| Value | Description |
|---|---|
| $string | Required. It is a mandatory/compulsory parameter in which we search the occurrence of the $search string. |
| $search | Required. It is also a mandatory parameter that specifies the string which is to be searched. |
| $start | Optional. It is the last and optional parameter of this function specifies that where to begin the search. This parameter has an integer value. |
Return Values
The PHP strrpos() in-build function returns the position of the last occurrence of a substring inside another string. If the string isn’t found, then it’ll return FALSE.
Changelogs
- PHP 5.0 included a new parameter $start in strrpos() function which defines that from where to begin the search.
- After PHP 5.0, we can also pass a string in the $search parameter rather than passing only a single character.
PHP strrpos() Examples
Let’s see the following example to learn the functionality which provides the basic knowledge of this function.
Example 1
|
1 2 3 4 5 6 |
<?php $string = "A smile is the universal welcome."; $search ='com'; $output1 = strrpos( $string, $search ); echo "The last occurrence of the search string is found at position: ".$output1; ?> |
Example 2
|
1 2 3 4 5 6 |
<?php $string = "A smile is the universal welcome."; $search ='v'; $output1 = strrpos( $string, $search ); echo "The last occurrence of the search string is found at position: ".$output1; ?> |
Example 3: Case-sensitive
|
1 2 3 4 5 6 7 8 |
<?php $string = "A smile is the universal welcome."; $search ='THE'; $result1 = strrpos( $string, $search ); echo $result1."</br>"; echo "Search string is not found, so it returned: "; var_dump($result1); ?> |
Example 4
In the following example, the search string is not available in the main string, so it will return the Boolean value FALSE.
|
1 2 3 4 5 6 7 8 |
<?php $string = "A smile is the universal welcome."; $search ='Tutorialscan'; $result1 = strrpos( $string, $search ); echo $result1."</br>"; echo "Search string is not found so it returned: "; var_dump($result1); ?> |
Here, Echo is not more sufficient to display the Boolean value, so here, we use the var_dump() function to print that Boolean value FALSE.
Example 5
The following example contains the if-else condition. If the string isn’t found, it’ll show that the search string isn’t found otherwise, it’ll display the position of the last occurrence of the search string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $string = "A smile is the universal welcome."; $search1 ='mal'; $search2="sal"; $result1 = strrpos( $string, $search1); if($result1==false) echo "Sorry! <b>". $search1. "</b> is not found in the string"; else echo "The following search string <b>". $search1. "</b> find at position: ".$result1; echo '</br>'; $result2 = strrpos( $string, $search2); if($result2==false) echo "Sorry! string is not found"; else echo "The following search string <b>". $search2. "</b> is found at position: ".$result2; ?> |
Sorry! mal is not found in the string
The following search string sal is found at position: 21
Example 6: By using the length parameter
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $string = "A smile is the universal welcome."; $search2="universal"; $position = strrpos($string, $search2, 10); if ($position == true){ echo "Found at position " . $position; } else{ echo "Search string is not found."; } ?> |
In the above example, “universal” is present in the main string; furthermore, still it displays the output that “search string is not found.” because the searching is started from the 17th position, but “universal” is available at the 15th position.
Example 7
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $string = "A smile is the universal welcome."; $search2="universal"; $position = strrpos($string, $search2, 11); if ($position == true){ echo "Found at position " . $position; } else{ echo "Search string is not found."; } ?> |
Conclusion
In this article, you have learned how to use PHP strrpos to finds the position of the last occurrence of a string inside another string. I hope you will enjoy it!. If have any problem, contact me on info@tutorialscan.com.