note 45172 added to function.strrpos

From: Date: Fri, 27 Aug 2004 21:11:41 +0000
Subject: note 45172 added to function.strrpos
Groups: php.notes 
Request: Send a blank email to php-notes+get-75464@lists.php.net to get a copy of this message
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; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.strrpos.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+45172 Delete -- http://master.php.net/manage/user-notes.php?action=delete+45172&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+45172&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#75464) next »