strpbrk
(PHP 5)
strpbrk — Ищет в строке любой символ из заданного набора
Описание
string strpbrk
( string $haystack
, string $char_list
)
strpbrk() ищет в строке haystack символы из набора char_list.
Список параметров
- haystack
-
Строка, в которой производится поиск char_list.
- char_list
-
Данный параметр чувствителен к регистру.
Возвращаемые значения
Возвращает строку, начиная с найденного символа, или FALSE, если он не был найден.
Примеры
Пример #1 Пример использования strpbrk()
<?php
$text = 'This is a Simple text.';
// Этот код выдаст "is is a Simple text.", т.к. символ 'i' встретится раньше
echo strpbrk($text, 'mi');
// Этот код выдаст "Simple text.", т.к. символы чувствительны к регистру
echo strpbrk($text, 'S');
?>
Смотрите также
- strpos() - Возвращает позицию первого вхождения подстроки
- strstr() - Находит первое вхождение подстроки
- preg_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
Коментарии
This functionality is now implemented in the PEAR package PHP_Compat.
More information about using this function without upgrading your version of PHP can be found on the below link:
http://pear.php.net/package/PHP_Compat
I wanted to use this function to look for an @ in a db entry - didn't work because I don't have this version of PHP yet, but I thought I had my issue licked. Darn it.For PHP versions before 5:
<?php
function strpbrk( $haystack, $char_list )
{
$strlen = strlen($char_list);
$found = false;
for( $i=0; $i<$strlen; $i++ ) {
if( ($tmp = strpos($haystack, $char_list{$i})) !== false ) {
if( !$found ) {
$pos = $tmp;
$found = true;
continue;
}
$pos = min($pos, $tmp);
}
}
if( !$found ) {
return false;
}
return substr($haystack, $pos);
}
?>
Sadly this is about ten times slower than the native implementation.
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:
<?php
function strpbrkpos($s, $accept) {
$r = FALSE;
$t = 0;
$i = 0;
$accept_l = strlen($accept);
for ( ; $i < $accept_l ; $i++ )
if ( ($t = strpos($s, $accept{$i})) !== FALSE )
if ( ($r === FALSE) || ($t < $r) )
$r = $t;
return $v;
}
?>
One undocumented requirement:If $char_list contains null characters ("\0"), only characters before the null will be used. While PHP handles nulls in strings just fine, the data is passed to a function that is not null safe.
A simpler (and slightly faster) strpbrkpos function:
<?php
function strpbrkpos($haystack, $char_list) {
$result = strcspn($haystack, $char_list);
if ($result != strlen($haystack)) {
return $result;
}
return false;
}
?>
Simple code to define symbol (@) in the string
<?php
$name = "username@example.com";
if (strpbrk($name, '@') != FALSE) {
echo "There is @";
}
else {
echo "There isn't @";
}
?>