strpbrk

(PHP 5, PHP 7, PHP 8)

strpbrkSearch a string for any of a set of characters

Description

strpbrk(string $string, string $characters): string|false

strpbrk() searches the string string for a characters.

Parameters

string

The string where characters is looked for.

characters

This parameter is case sensitive.

Return Values

Returns a string starting from the character found, or false if it is not found.

Examples

Example #1 strpbrk() example

<?php

$text
= 'This is a Simple text.';

// this echoes "is is a Simple text." because 'i' is matched first
echo strpbrk($text, 'mi');

// this echoes "Simple text." because chars are case sensitive
echo strpbrk($text, 'S');
?>

See Also

  • strpos() - Find the position of the first occurrence of a substring in a string
  • strstr() - Find the first occurrence of a string
  • preg_match() - Perform a regular expression match

add a note add a note

User Contributed Notes 2 notes

up
14
devnuhl
10 years ago
If you're not looking to duplicate the rest of the string, but instead just want the offset, in the spirit of the str*pos() functions, use strcspn()
up
5
guillaume dot barranco at free dot fr
7 years ago
A little modification to Evan's code to use an array for the second parameter :

<?php

function strpbrkpos($s, $accept) {
 
$r = FALSE;
 
$t = 0;
 
$i = 0;
 
$accept_l = count($accept);

  for ( ;
$i < $accept_l ; $i++ )
    if ( (
$t = strpos($s, $accept[$i])) !== FALSE )
      if ( (
$r === FALSE) || ($t < $r) )
       
$r = $t;

    return
$r;
}

?>
To Top