note 71824 added to function.strpos

From: Date: Mon, 18 Dec 2006 10:31:42 +0000
Subject: note 71824 added to function.strpos
Groups: php.notes 
Request: Send a blank email to php-notes+get-120719@lists.php.net to get a copy of this message
I've been looking at previous posts and came up with this function to find the start and end off an certain occurance or all occurances of needle within haystack. I've made some minor tweaks to the code itself, like counting the length of needle only once and counting the result set array instead of using a count variable. I also added a length parameter to the result set to use in a following substr_replace call etc... <?php function strpos_index($haystack = '',$needle = '',$offset = 0,$limit = 99,$return = null) { $length = strlen($needle); $occurances = array(); while((($count = count($occurances)) < $limit) && (false !== ($offset = strpos($haystack,$needle,$offset)))) { $occurances[$count]['length'] = $length; $occurances[$count]['start'] = $offset; $occurances[$count]['end'] = $offset = $offset + $length; } return $return === null ? $occurances : $occurances[$return]; } ?> ---- Server IP: 194.109.193.94 Probable Submitter: 80.126.41.235 ---- Manual Page -- http://www.php.net/manual/en/function.strpos.php Edit -- https://master.php.net/note/edit/71824 Del: integrated -- https://master.php.net/note/delete/71824/integrated Del: useless -- https://master.php.net/note/delete/71824/useless Del: bad code -- https://master.php.net/note/delete/71824/bad+code Del: spam -- https://master.php.net/note/delete/71824/spam Del: non-english -- https://master.php.net/note/delete/71824/non-english Del: in docs -- https://master.php.net/note/delete/71824/in+docs Del: other reasons-- https://master.php.net/note/delete/71824 Reject -- https://master.php.net/note/reject/71824 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#120719) next »