Si possible, n'utilisez que des fonctions PHP standard telles que substr(), strrpos(), strpos(), etc.
Réponses
Trop de publicités?
Matthew Flaschen
Points
131723
Gumbo
Points
279147
ilya n.
Points
6610
Martin Tilsted
Points
489
Je ne pense pas que cela soit possible avec strrpos parce que le début de la position n'a pas été défini. pas de la façon dont on s'y attendrait.
Il n'y a pas, à première vue, de solution évidente, mais cette fonction devrait permettre d'y parvenir. (Elle n'a pas été testée, mais je pense qu'elle fonctionne).
/** Return the position of the second last needle in haystack or false if nothing is found. I really hate functions with return types that depend on their input, but this function is made to look like the similary php functions (strpos, strrpos and so on) */
// Needle must be a char. If you want it to be a string instead, just substr
// length of needle instead of 1 in this example.
function findSecondLastChar($needle,$haystack) {
$found=0;
for($pos=strlen($haystack)-1;$pos>=0;$pos--) {
if(substr($haystack,$pos,1)==$needle) {
if($found++ == 1)
return $pos;
}
}
// If we reach this, nothing were found
return false;
}
- Réponses précédentes
- Plus de réponses