Facebook Page Twitter Page LinkedIn Page
× PHP Tutorial PHP Advanced PHP Resources


The PHP strpos() function is used to return the index number of first occurrence of specified text in the given string. Unlike stripos function, it is a case-sensitive function.

Note: The strpos() function is a case-sensitive function.

Syntax

strpos(string, find, start)

Parameters

string Required. Specify the string to search.
find Required. Specify the string to find.
start Optional. Specify the index number from where the search need to start.

Return Value

Returns the index number of first occurrence of specified text in the given string.

Example:

In the below example, strpos() function to used to find the index number of first occurrence of "be" in the given string called MyString.

<?php
  $MyString = "To be, or not to be, that is the question.";

  echo strpos($MyString, "be")."\n";
?>

The output of the above code will be:

3

Example:

In the below example, first occurrence of "be" is searched after the specified strating position.

<?php
  $MyString = "To be, or not to be, that is the question.";

  echo strpos($MyString, "be", 5)."\n";
?>

The output of the above code will be:

17

❮ PHP String functions