How to Find a Substring’s Position with PHP strpos()

PHP’s strpos() returns the zero-based byte position of the first case-sensitive match in a string. It returns false when the substring is absent. Use a strict comparison with false so that a match at the start of the string counts as a success.

Arguments and return values

The first argument to PHP manual for strpos(), $haystack, is the string to search. The second, $needle, is the substring to look for. The optional $offset specifies the byte at which to start searching and defaults to 0.

<?php
$text = '123456789';

var_dump(strpos($text, '1'));
var_dump(strpos($text, '2'));
var_dump(strpos($text, 'A'));
Substring Return value
'1' int(0)
'2' int(1)
'A' bool(false)

Run these examples with PHP 8. Save each code block as a separate PHP file and run it from a terminal with its filename, for example php find-position.php. The first example produces the output shown in the table.

Distinguish position 0 from false

A condition such as if ($position) treats the integer 0 as false. Use $position === false to detect a missing match or $position !== false to detect any match.

<?php
$text = '123456789';
$position = strpos($text, '1');

if ($position === false) {
    echo "Not found", PHP_EOL;
} else {
    echo $position, PHP_EOL;
}

This example prints 0 because 1 occurs at the start of the string.

Set the search offset

In 123454321 the digit 3 occurs at positions 2 and 6. An offset of 0 finds the first one. An offset of 3 starts at the fourth byte, and -3 starts at the third byte from the end. The returned position is always measured from the start of the string.

<?php
$text = '123454321';

var_dump(strpos($text, '3', 0));
var_dump(strpos($text, '3', 3));
var_dump(strpos($text, '3', -3));
var_dump(strpos('abc', '', 3));

The calls print int(2), int(6), int(6), and int(3). PHP 8 accepts an empty needle, so the last call returns the search starting position, 3.

Valid offsets range from -strlen($haystack) through strlen($haystack), inclusive. In PHP 8, a value outside that range throws a ValueError. An offset equal to the length is valid and returns false for a nonempty needle.

Ignore case or check for containment

Use PHP manual for stripos() for case-insensitive position searches. As of PHP 8.2, its case folding applies to ASCII letters. In PHP 8 or later, use the case-sensitive PHP manual for str_contains() when you only need a boolean for containment.

<?php
$text = 'FooBar';

var_dump(strpos($text, 'foo'));
var_dump(stripos($text, 'foo'));
var_dump(str_contains($text, 'Foo'));

Searching for foo gives bool(false) with strpos() and int(0) with stripos(). Calling str_contains() with Foo gives bool(true).

Find a character position in UTF-8

UTF-8 characters can occupy different numbers of bytes. To get a character index, use PHP manual for mb_strpos() with UTF-8 and the mbstring extension. Save the last example as UTF-8 and run php --ri mbstring to check that the extension is enabled.

<?php
$text = 'é猫A';

var_dump(strpos($text, 'A'));
var_dump(mb_strpos($text, 'A', 0, 'UTF-8'));

The character é takes two bytes and 猫 takes three. A therefore starts at byte index 5 and character index 2. If you pass the position to a slicing function, use the same unit in that function.