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;
}
?>
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');
?>
Evan ¶
5 years ago
root at mantoru dot de ¶
5 years ago
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;
}
?>
