strpbrk()
The strpbrk() function in PHP is used to search a string for any of a set of characters. It returns the portion of the string that starts with the first
Introduction
The strpbrk() function in PHP searches a string for any one of a set of characters and returns the rest of the string starting from the first match. The name stands for "string pointer break" — it scans the haystack left to right and stops (breaks) the moment it hits a character that appears in your search set.
This makes it handy for jobs like "give me everything from the first @, =, or ? onward" without writing a regular expression. This article covers the syntax, return values, a common gotcha around how the match is chosen, and practical use cases.
Syntax
strpbrk(string $haystack, string $char_list): string|false| Parameter | Description |
|---|---|
$haystack | The string to search in. |
$char_list | A string listing the characters to look for. Each character is treated individually — "abc" means "any of a, b, or c", not the substring "abc". |
Return value: the portion of $haystack from the first matching character to the end of the string. If none of the characters in $char_list are present, it returns false.
strpbrk() is case-sensitive: searching for "A" will not match a lowercase a.
Basic example
Output:
Found 'llo World' in 'Hello World'A common surprise here: the result is llo World, not World. The function returns from the first matching character it meets while scanning left to right. Although W is in the search list, the lowercase l in "Hello" comes earlier in the string, so the slice begins there. Order in $char_list does not matter — only position in the haystack does.
The match is positional, not list-ordered
It is easy to assume strpbrk() honors the order of characters you pass. It does not — it returns from whichever search character appears earliest in the haystack:
<?php
// 'o' and 'q' are both in the list; 'q' appears first in the haystack.
echo strpbrk("The quick brown fox", "oq"); // quick brown foxOutput:
quick brown foxPractical use: split on a delimiter
Because strpbrk() returns "everything from the delimiter onward", you can pair it with substr() to grab the value after a separator:
<?php
$pair = "name=John";
$fromEquals = strpbrk($pair, "="); // "=John"
$value = substr($fromEquals, 1); // drop the leading "=" -> "John"
echo $value;Output:
JohnIt also works well when you accept several possible delimiters at once:
<?php
$line = "[email protected]";
echo strpbrk($line, "@?#"); // @example.comOutput:
@example.comHandling "not found"
When no listed character exists in the haystack, strpbrk() returns false. Always compare with the strict !== operator so an empty-but-valid result is not mistaken for failure:
<?php
$result = strpbrk("PHP 8.4", "AEIOU"); // uppercase vowels — none present (case-sensitive)
if ($result === false) {
echo "No matching character found.";
} else {
echo $result;
}Output:
No matching character found.strpbrk() vs. related functions
| Function | Searches for | Returns |
|---|---|---|
strpbrk() | any of several characters | the rest of the string from the first match, or false |
| strpos() | a single substring | the numeric position of the match, or false |
| strstr() | a single substring | the rest of the string from that substring, or false |
| strrchr() | the last occurrence of a character | the rest of the string from that point, or false |
Reach for strpbrk() when you have a small set of characters to react to and you want the tail of the string. If you only ever search for one fixed substring, strstr() or strpos() is clearer. For pattern-based matching, use preg_match().
Conclusion
strpbrk() returns the remainder of a string starting at the first character that matches any character in your search set, or false if there is no match. Remember that the match is chosen by position in the haystack, not by the order of your search list, and that it is case-sensitive. It is a concise alternative to a regular expression whenever you need "everything from the first delimiter onward".