How to Find Position of Last Occurrence of a String Inside Another String in PHP?

How to Find Position of Last Occurrence of a String Inside Another String in PHP?

  • PHP
  • 1 min read

HP program to find the position of the last occurrence of a string inside another string.

There is predefined function strrpos(), which helps to find the position of the last occurrence of a string within another string. It will return false if the string is not found.

Syntax of strrpos():

strrpos(arg1,arg2,arg3)

Here the first argument(arg1) is the string in which the search should be performed, the second argument(arg2)is the substring for which to search, and the third argument(arg3) is used to specify the point in the string to initiate the search. The third argument is optional.

Now we will give an example for finding the position of the last occurrence of a string "India" in a string "welcome to India. India is great."

<html>
<body>
<?php
$a="welcome to India.India is great";
$b=strpos($a,"India");
echo $b;
?>
</body>
</html>

Output: 17

See also: