strpbrk

(PHP 5, PHP 7, PHP 8)

strpbrk文字列の中から任意の文字を探す

説明

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

strpbrk() は、文字列 string から characters を探します。

パラメータ

string

characters を探す文字列。

characters

このパラメータは大文字小文字を区別します。

戻り値

見つかった文字から始まる文字列、あるいは見つからなかった場合に 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() - 正規表現によるマッチングを行う

add a note

User Contributed Notes 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
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 :<?phpfunction 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