PHP Program to Find Position of First Occurrence of a String Inside Another String?

PHP Program to Find Position of First Occurrence of a String Inside Another String?

  • PHP
  • 1 min read

Here is an example of a PHP program to find the position of the first occurrence of a string inside another string?

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

Syntax of strpos():

strpos(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.

Here is an example for finding the position of the first 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: 11

See also: