Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Yes—PHP’s strpos() accepts a multi-character substring as its $needle. It returns the zero-based position of the first match, or false if there is no match. Use === false to check for a miss, because a match at position 0 is valid. The PHP manual documents the function’s signature and behavior.

Search for a substring with strpos()

The $needle argument is the string to find; it can contain one character or many. For example:

<?php
$haystack = 'The quick brown fox';
$needle = 'brown';

$position = strpos($haystack, $needle);

if ($position === false) {
    echo 'Not found';
} else {
    echo "Found at byte position $position";
}

With this haystack, strpos() returns the position where the first occurrence of brown begins. Its result is an integer position or false when there is no match. Because position zero is a possible match, a loose check such as if (!$position) incorrectly treats a match at the start of the string as a miss.

Understand positions, offsets, and case sensitivity

Positions are zero-based and measured from the beginning of the haystack. The optional offset changes where searching starts, but a match position is still reported relative to the beginning. A negative offset, supported since PHP 7.1.0, counts backward from the end of the string. An offset greater than the haystack’s length throws a ValueError.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The search is case-sensitive: for example, a needle containing Brown does not match brown. If the caller only needs a yes-or-no containment result, PHP 8’s str_contains($haystack, $needle) returns a boolean instead of a position. See the PHP manual entry for str_contains().

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle empty needles and older argument forms

In PHP 8 and later, an empty needle is accepted and matches at every position: strpos($haystack, '') returns 0, while a supplied offset is returned when one is given. If an empty search term should count as invalid input in your application, check for it explicitly before calling strpos().

The current signature is strpos(string $haystack, string $needle, int $offset = 0): int|false. Integer needles were deprecated in PHP 7.3.0 and are no longer supported in PHP 8.0.0. If an integer represents a character ordinal, convert it with chr(); if it is meant to be text, cast it to a string. These behaviors and version notes are listed in the official strpos() documentation.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.