PHP 8.4.23 Released!

strpbrk

(PHP 5, PHP 7, PHP 8)

strpbrk在字符串中查找一组字符的任何一个字符

说明

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

strpbrk()string 字符串中查找 characters

参数

string

在此字符串中查找 string

characters

该参数区分大小写。

返回值

返回以找到的字符开始的子字符串。如果没有找到,则返回 false

示例

示例 #1 strpbrk() 示例

<?php
$text = 'This is a Simple text.';
// 输出 "is is a Simple text.",因为 'i' 先被匹配
echo strpbrk($text, 'mi'), PHP_EOL;
// 输出 "Simple text.",因为字符区分大小写
echo strpbrk($text, 'S'), PHP_EOL;
?>

参见

发现了问题?

了解如何改进此页面提交拉取请求报告一个错误
+添加备注

用户贡献的备注 2 notes

up
14
devnuhl
11 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
2
guillaume dot barranco at free dot fr
8 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;
}
?>
+添加备注

AltStyle によって変換されたページ (->オリジナル) /