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 — Recherche un ensemble de caractères dans une chaîne de caractères
strpbrk() recherche dans la chaîne string
n'importe lequel des caractères de characters.
string
La chaîne dans laquelle on cherche characters.
charactersCe paramètre est sensible à la casse.
Retourne une chaîne, commençant au premier caractère trouvé,
ou false s'il n'a pas été trouvé.
Lève une exception ValueError si
characters est vide.
| Version | Description |
|---|---|
| 8.0.0 |
Lève désormais une exception ValueError
si characters est vide.
Auparavant, une E_WARNING était émise et false
était retourné.
|
Exemple #1 Exemple avec strpbrk()
<?php
$text = 'This is a Simple text.';
// Ceci affichera "is is a Simple text." car 'i' correspond au premier
echo strpbrk($text, 'mi'), PHP_EOL;
// Ceci affichera "Simple text." car les caractères sont sensibles à la casse
echo strpbrk($text, 'S'), PHP_EOL;
?>
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;
}
?>