strpbrk

Returns the rest of a string starting at the first occurrence of any of the given characters.

Signature
strpbrk(string $string, string $characters): string|false
  • Added in PHP 5.0

Parameters

ParameterTypeDescription
$stringstringThe string where characters is looked for.
$charactersstringThis parameter is case sensitive.

Return value

string|falseA string starting from the character found, or false if it is not found.

strpbrk() scans a string left to right and stops at the first byte that appears anywhere in characters. It then returns the remainder of the string from that byte onwards, or false if none of the characters occur. The name comes from C's strpbrk: "string pointer break".

When to reach for it

It answers "where does this string stop being simple?" in one call. Finding the first delimiter in a hand-rolled parser, spotting the first character that needs escaping, or splitting a value at whichever of several separators appears first are all natural uses. Compared with a loop of strpos() calls, one per candidate character, it is a single pass.

What trips people up

It returns a substring, not a position. To get the offset, subtract lengths: strlen($string) - strlen($result). If you only ever wanted the offset, strcspn() gives it directly and never returns false.

The characters argument is a set of individual bytes, not a substring to find and not a regular expression — strpbrk($s, 'abc') looks for a or b or c. It is also case sensitive, so include both cases if you need them.

Test the result with === false: a found character at the very end returns a one-byte string, which is truthy, but a match on '0' returns '0', which is not.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Find the first delimiter in a line when several separators are possible.
  • Detect whether a filename or identifier contains any forbidden character.
  • Split a token from the rest of a string at the first punctuation mark.

Frequently asked questions

What is the difference between strpbrk and strpos?

strpos searches for one whole substring and returns its position. strpbrk searches for any one of a set of single characters and returns the rest of the string from the first hit.

How do I get the position instead of the substring from strpbrk?

Subtract the lengths: strlen($string) - strlen($result). If the position is all you need, use strcspn($string, $characters), which returns the offset directly.

Does strpbrk accept a regular expression?

No. The second argument is a plain list of individual bytes to look for. Use preg_match() with a character class if you need pattern matching.

More examples

Splitting at the first of several delimiters

The semicolon is reached before the ampersand, so $rest is ";role=admin&active=1" and the head is "user=alice". Subtracting the two lengths recovers the split point without knowing which delimiter won.

Example · php
<?php
$line = 'user=alice;role=admin&active=1';

$rest = strpbrk($line, ';&');
$head = substr($line, 0, strlen($line) - strlen($rest));

echo 'first field: ', $head, PHP_EOL;
echo 'remainder:   ', $rest, PHP_EOL;

Checking for forbidden characters

report.txt passes, while "my report.txt" reports an illegal character at offset 2 (the space) and draft/v2.txt at offset 5 (the slash). The $bad string is a set of individual bytes, so the escaped backslash matches a single backslash.

Example · php
<?php
$names = ['report.txt', 'my report.txt', 'draft/v2.txt'];
$bad   = " /\\?*:";

foreach ($names as $n) {
    $hit = strpbrk($n, $bad);
    echo $n, ' => ', $hit === false
        ? 'ok'
        : 'illegal char at ' . (strlen($n) - strlen($hit)), PHP_EOL;
}

Discussion

  • Be the first to comment on this lesson.