strpbrk
(PHP 5)
strpbrk — Search a string for any of a set of characters
Description
string strpbrk
( string
$haystack
, string $char_list
)
strpbrk() searches the haystack
string for a char_list
.
Parameters
-
haystack
-
The string where
char_list
is looked for. -
char_list
-
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
- addcslashes
- addslashes
- bin2hex
- chop
- chr
- chunk_split
- convert_cyr_string
- convert_uudecode
- convert_uuencode
- count_chars
- crc32
- crypt
- echo
- explode
- fprintf
- get_html_translation_table
- hebrev
- hebrevc
- hex2bin
- html_entity_decode
- htmlentities
- htmlspecialchars_decode
- htmlspecialchars
- implode
- join
- lcfirst
- levenshtein
- localeconv
- ltrim
- md5_file
- md5
- metaphone
- money_format
- nl_langinfo
- nl2br
- number_format
- ord
- parse_str
- printf
- quoted_printable_decode
- quoted_printable_encode
- quotemeta
- rtrim
- setlocale
- sha1_file
- sha1
- similar_text
- soundex
- sprintf
- sscanf
- str_getcsv
- str_ireplace
- str_pad
- str_repeat
- str_replace
- str_rot13
- str_shuffle
- str_split
- str_word_count
- strcasecmp
- strchr
- strcmp
- strcoll
- strcspn
- strip_tags
- stripcslashes
- stripos
- stripslashes
- stristr
- strlen
- strnatcasecmp
- strnatcmp
- strncasecmp
- strncmp
- strpbrk
- strpos
- strrchr
- strrev
- strripos
- strrpos
- strspn
- strstr
- strtok
- strtolower
- strtoupper
- strtr
- substr_compare
- substr_count
- substr_replace
- substr
- trim
- ucfirst
- ucwords
- vfprintf
- vprintf
- vsprintf
- wordwrap
Коментарии
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()
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;
}
?>