Overview of PHP strpos function

The strpos is in-built PHP function. The strpos function finds the position of the first occurrence of a string in another string. It returns the integer number as the position of the first occurrence of the string. The function strpos is case sensitive.

Note: The position starts from 0 and not 1.

Syntax:

	strpos(originalstring,searchstring,position)

Parameters:

  • originalstring(required): The string in which we need to search for a string.
  • searchstring(required): The string which needs to find in the originalstring
  • position(optional): The position from where the search must begin. The default position is 0.

Example:

<?php
    $string = "We are at thecodetutorial"; 
    $search = "thecodetutorial"; 
    $positionafterfour = strpos($string, $search, 4);
    $positionfromstart = strpos($string, $search, 0);
    echo "Found at position(position from: 4): " . $positionafterfour."<br/>";
    echo "Found at position from  start string: " . $positionfromstart;
    exit;
?>

Output:

Found at position(position from: 4): 10
Found at position from start string: 10