Computer >> Computer tutorials >  >> Programming >> PHP

strpos() function in PHP


The strpos() function is used to find the position of first occurrence of a string inside another string.

Syntax

strpos(str, find, begin)

Parameters

  • str − The string to search

  • find − The string to find

  • begin − Where to begin the search

Return

The strpos() function returns the position of first occurrence of a string inside another string or else false if string is not found.

Example

The following is an example −

<?php
   echo strpos("This is demo text !","demo");
?>

Output

The following is the output −

8

Example

Let us see another example −

<?php
   $str = 'pqr pqr';
   $pos = strpos($str, 'p', 1);
   echo $pos;
?>

Output

The following is the output −

4