|
| HOW TO USE THE strrpos() FUNCTION IN PHP
The strrpos() function is used to find the last occurrence of a string inside another string. If found, the function returns, as an integer, the position of the last occurrence of that string searched for.
If no strings found, the function returns false. Please bear in mind that the strrpos() function is case sensitive. Also note the characters are counted in a string numerically where the first character is position 0.
The general format is:
strrpos(string to search, string to find, starting position);
string to search - The string to perform the search.
string to find - The string to find.
starting position = Integer. Optional. Indicates position to start search from. If not set, position starts from beginning of string by default.
Example Without The Optional Start Position
$string_to_search="My name is Wendy Moss";
$string_to_find="n";
$position=strrpos($string_to_search,$string_to_find);
echo $position;
The above outputs: 13 as n occurs last at character position 13 (occurs first at position 3).
Example With The Optional Start Position
$string_to_search="My name is Wendy Moss. I am not Selena Smith!";
$string_to_find="n";
$position=strrpos($string_to_search,$string_to_find, 4);
echo $position;
The above outputs: 36 as n is last found at position 36.
See also:
|
|