note 108426 added to function.strpos

From: Date: Wed, 25 Apr 2012 16:46:54 +0000
Subject: note 108426 added to function.strpos
Groups: php.notes 
Request: Send a blank email to php-notes+get-187341@lists.php.net to get a copy of this message
when you want to know how much of substring occurrences, you'll use "substr_count". But, retrieve their positions, will be harder. So, you can do it by starting with the last occurrence : function strpos_r($haystack, $needle) { if(strlen($needle) > strlen($haystack)) trigger_error(sprintf("%s: length of argument 2 must be <= argument 1", __FUNCTION__), E_USER_WARNING); $seeks = array(); while($seek = strrpos($haystack, $needle)) { array_push($seeks, $seek); $haystack = substr($haystack, 0, $seek); } return $seeks; } it will return an array of all occurrences a the substring in the string Example : $test = "this is a test for testing a test function... blah blah"; var_dump(strpos_r($test, "test")); // output array(3) { [0]=> int(29) [1]=> int(19) [2]=> int(10) } Paul-antoine Malézieux. ---- Server IP: 89.31.146.176 Probable Submitter: 82.236.25.214 ---- Manual Page -- http://www.php.net/manual/en/function.strpos.php Edit -- https://master.php.net/note/edit/108426 Del: integrated -- https://master.php.net/note/delete/108426/integrated Del: useless -- https://master.php.net/note/delete/108426/useless Del: bad code -- https://master.php.net/note/delete/108426/bad+code Del: spam -- https://master.php.net/note/delete/108426/spam Del: non-english -- https://master.php.net/note/delete/108426/non-english Del: in docs -- https://master.php.net/note/delete/108426/in+docs Del: other reasons-- https://master.php.net/note/delete/108426 Reject -- https://master.php.net/note/reject/108426 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#187341) next »