note 68073 deleted from function.strpos by danbrown

From: Date: Wed, 03 Aug 2011 13:18:06 +0000
Subject: note 68073 deleted from function.strpos by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-182163@lists.php.net to get a copy of this message
Note Submitter: chasesan at gmail dot com ---- You can use strpos to produce a funciton that will find the nth instance of a certain string within a string. Personally I find this function almost more useful then strpos itself. I kinda wish they would put it stock into php but I doupt thats gonna happen any time soon. ^_^ Here is da code: <?php //just like strpos, but it returns the position of the nth instance of the needle (yay!) function strpos2($haystack, $needle, $nth = 1) { //Fixes a null return if the position is at the beginning of input //It also changes all input to that of a string ^.~ $haystack = ' '.$haystack; if (!strpos($haystack, $needle)) return false; $offset=0; for($i = 1; $i < $nth; $i++) $offset = strpos($haystack, $needle, $offset) + 1; return strpos($haystack, $needle, $offset) - 1; } ?>

« previous php.notes (#182163) next »