October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

PHP strpos() with Multiple Characters: Substring Search and Examples

PHP strpos() accepts multi-character needles and returns the first match’s zero-based position. Learn how to test results and handle offsets, case, and empty needles.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Yes. PHP’s strpos() can search for a multi-character substring: pass the substring as $needle. It returns the zero-based position of the first match, or false if there is no match. Use === false to check for failure, because a match at the start of the string returns 0.

How to search for a multi-character substring

The $needle parameter is a string, so it can contain one character or a longer sequence. strpos() finds the first occurrence of that sequence in the haystack.

<?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";
}

For this example, the function returns 10, the zero-based position where brown begins. The PHP manual documents the signature as strpos(string $haystack, string $needle, int $offset = 0): int|false and describes it as finding the numeric position of the first occurrence of the needle. See the PHP strpos() manual.

Check the result without confusing position 0 for failure

A match at the beginning of the haystack has position 0. Because 0 is false-like in PHP, a loose check such as if (!$position) incorrectly treats that valid match as missing. Compare strictly with false:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
if ($position === false) {
    // No match
} else {
    // Match found, including at position 0
}

The returned position is zero-based. If no occurrence exists, the return value is the boolean false.

Case sensitivity and a boolean alternative

strpos() is case-sensitive: searching for 'Brown' will not match 'brown'. If you only need to know whether a substring occurs, rather than its position, PHP 8 and later provide str_contains(), which returns a boolean and is also case-sensitive.

if (str_contains($haystack, 'brown')) {
    echo 'Found';
}

Use strpos() when the position matters; use str_contains() for a yes-or-no containment check. See the PHP str_contains() manual.

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

Offsets and edge cases

The optional offset controls where the search starts. A positive offset begins at that position. Negative offsets are supported starting with PHP 7.1.0 and search from a point counted back from the end of the string; any returned position is still relative to the beginning of the haystack. 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.

In PHP 8 and later, an empty needle is valid: it matches at every position. With no offset, strpos() returns 0; with an offset, it returns that offset. If an empty needle should count as invalid in your application, reject it explicitly before calling the function.

The needle should be a string. Integer needles were deprecated in PHP 7.3.0 and are no longer supported as of PHP 8.0.0. If an integer represents a character ordinal, use chr() explicitly; if you mean its digits as text, cast it to a string.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.