PHP strpbrk() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, strpbrk() searches a string for any one of several characters and returns the rest of the string from the first match.

Think of it as "find the first character from this set". It is case-sensitive.

Syntax

Call strpbrk() with the values you need. Start with this line.

PHP
$rest = strpbrk("This is a test", "st");
CallWhat it does
strpbrk("This is a test", "st")Returns "s is a test".
strpbrk("[email protected]", "@.")Returns "@example.com".
strpbrk("hello", "xyz")Returns false because no character matches.

The second argument is a list of single characters, not a word. Each character is searched on its own. The result starts at the earliest position of any of them.

Library declarations
PHP
strpbrk(string $string, string $characters): string|false

Parameters

ParameterDescription
stringRequired. The string to search in.
charactersRequired. A string of characters. The function finds the first one that appears.

Return Value

Return ValueDescription
stringThe part of the string starting at the first matching character.
falseReturned when none of the characters are found.

PHP Examples

Example 1 — Find the First Matching Character

Look for the first "s" or "t" in a sentence.

PHP
<?php
$text = "This is a test";
$rest = strpbrk($text, "st");

echo $rest, PHP_EOL;

How It Works

  1. Create a sentence to search.
  2. Pass the characters "s" and "t" as one string.
  3. PHP finds the earliest match: the lowercase "s" in "This".
  4. It returns everything from that point to the end.

Example 2 — No Match Returns false

When none of the characters appear, you get false.

PHP
<?php
$result = strpbrk("hello", "xyz");

var_dump($result);

How It Works

  1. Search "hello" for x, y, or z.
  2. None of those letters appear.
  3. strpbrk returns false.
  4. var_dump shows the exact result type.

Example 3 — Split Off an Email Domain

Use a set of separators to find where the domain part starts.

PHP
<?php
$email = "[email protected]";
$rest = strpbrk($email, "@.");

echo $rest, PHP_EOL;

How It Works

  1. Store an email address.
  2. Search for either "@" or ". ".
  3. The "@" comes first in the string.
  4. The result starts from "@" and goes to the end.

Frequently Asked Questions

strpbrk() searches a string for any character from a list and returns the substring starting at the first one it finds.
strstr() searches for a whole substring. strpbrk() searches for any single character from a set of characters.
Yes. Searching for 'S' will not match 's'. Include both cases in the character list if you need both.
It returns false. Use === false to check, since an empty string is also falsy.
strpbrk() returns text, not a position. Use strcspn($string, $characters) to get the position of the first matching character.

Next: strpos()

strpos() finds the position of the first occurrence of a substring inside a string.

strpos() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful