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 −

 Live Demo

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

Output

The following is the output −

8

Example

Let us see another example −

 Live Demo

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

Output

The following is the output −

4

Updated on: 26-Dec-2019

251 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements