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 — Recherche un ensemble de caractères dans une chaîne de caractères
Description
string strpbrk
( string
$haystack
, string $char_list
)
strpbrk() recherche l'ensemble de caractères
char_list dans la chaîne haystack.
Liste de paramètres
-
haystack -
La chaîne dans laquelle on cherche
char_list. -
char_list -
Ce paramètre est sensible à la casse.
Valeurs de retour
Retourne une chaîne, commençant au premier caractère trouvé,
ou FALSE s'il n'a pas été trouvé.
Exemples
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');
// Ceci affichera "Simple text." car les caractères sont sensibles à la casse
echo strpbrk($text, 'S');
?>
Voir aussi
- strpos() - Cherche la position de la première occurrence dans une chaîne
- strstr() - Trouve la première occurrence dans une chaîne
- preg_match() - Expression rationnelle standard
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;
}
?>
