note 45172 deleted from function.strrpos by aidan

From: Date: Sat, 28 Aug 2004 03:54:53 +0000
Subject: note 45172 deleted from function.strrpos by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-75480@lists.php.net to get a copy of this message
Note Submitter: ---- My goodness, another strpos from right function. But: it behaves EXACTLY like strpos only that it works from right to left, it's pretty and it is speed optimised. You can always use it instead of strrpos() (even if your needle is only one char long) because it is not much slower and it has this nice "start from position $pos" option. Enjoy: <?php function strposr($hay,$needle,$pos=0) { // exactly like strpos() but searches from the right // very fast // (C) 2004 BIS | Gordon Axmann if ($pos>0) $hay=substr($hay,0,$pos+1); if (strlen($needle)<2) return strrpos($hay,$needle); // check if strrpos can do the work $i=0; $max=0; $plus='+'; do { $i=strpos($plus.$hay,$needle,$max+1); if ($i>0) $max=$i; } while ($i>0); if ($max==0) return false; return $max-1; } ?>

« previous php.notes (#75480) next »