总有人间一两风,填我十万八千梦

PHP strpbrk 用法 手册 | 示例代码

PHP开发手册 归档 559℃ 0评论

strpbrk

(PHP 5, PHP 7)

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

说明

strpbrk ( string $haystack , string $char_list ) : string

strpbrk() 函数在 haystack 字符串中查找 char_list 中的字符。

参数

haystack

在此字符串中查找 char_list

char_list

该参数区分大小写。

返回值

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

范例

Example #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() – 执行匹配正则表达式

用户贡献的笔记

devnuhl

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()

guillaume dot barranco at free dot fr

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;
}

?>

转载请注明:悠然品鉴 » PHP strpbrk 用法 手册 | 示例代码

喜欢 (0)or分享 (0)
发表我的评论
取消评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址