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()
(PHP 5, PHP 7)
strpbrk — 文字列の中から任意の文字を探す
$haystack
, string $char_list
) : string
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');
?>
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()
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;
}
?>
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;
}
?>