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