String Functions

PHP strpos() Function

Topic: PHP String ReferencePrev|Next

Description

The strpos() function finds the position of the first occurrence of a string inside another string.

This function is case-sensitive. For case-insensitive searches, use the stripos() function.

The following table summarizes the technical details of this function.

Return Value: Returns the position of the first occurrence of a string within another string, or FALSE if the string was not found. Remember that string positions start at 0, not 1.
Changelog: Since PHP 7.1.0, the start parameter can take negative values.
Version: PHP 4+

Syntax

The basic syntax of the strpos() function is given with:

strpos(string, search, start);

The following example shows the strpos() function in action.

<?php
// Sample strings
$str = "The woodpeckers live in the woods.";
$substr = "the wood";

// Performing search
echo strpos($str, $substr);
?>

Parameters

The strpos() function accepts the following parameters.

Parameter Description
string Required. Specifies the string to search in.
search Required. Specifies the string to search for.
start Optional. Specifies the position in the string to start searching from. If it is negative, the search will start this number of characters counted from the end of the string.

More Examples

Here're some more examples showing how strpos() function actually works:

The following example uses the start parameter to perform searching.

<?php
// Performing search
echo strpos("The woodpeckers live in the woods.", "wood", 3); 
?>

In the following example the start parameter is set to a negative value.

<?php
// Performing search
echo strpos("The woodpeckers live in the woods.", "wood", -10);
?>
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties