note 106407 added to function.strpos

From: Date: Tue, 08 Nov 2011 03:59:18 +0000
Subject: note 106407 added to function.strpos
Groups: php.notes 
Request: Send a blank email to php-notes+get-184322@lists.php.net to get a copy of this message
A pair of functions to replace every nth occurrence of a string with another string, starting at any position in the haystack. The first works on a string and the second works on a single-level array of strings, treating it as a single string for replacement purposes (any needles split over two array elements are ignored). Can be used for formatting dynamically-generated HTML output without touching the original generator: e.g. add a newLine class tag to every third item in a floated list, starting with the fourth item. <?php /* String Replace at Intervals by Glenn Herbert (gjh42) 2010-12-17 */ //(basic locator by someone else - name unknown) //strnposr() - Find the position of nth needle in haystack. function strnposr($haystack, $needle, $occurrence, $pos = 0) { return ($occurrence<2)?strpos($haystack, $needle, $pos):strnposr($haystack,$needle,$occurrence-1,strpos($haystack, $needle, $pos) + 1); } //gjh42 //replace every nth occurrence of $needle with $repl, starting from any position function str_replace_int($needle, $repl, $haystack, $interval, $first=1, $pos=0) { if ($pos >= strlen($haystack) or substr_count($haystack, $needle, $pos) < $first) return $haystack; $firstpos = strnposr($haystack, $needle, $first, $pos); $nl = strlen($needle); $qty = floor(substr_count($haystack, $needle, $firstpos + 1)/$interval); do { //in reverse order $nextpos = strnposr($haystack, $needle, ($qty * $interval) + 1, $firstpos); $qty--; $haystack = substr_replace($haystack, $repl, $nextpos, $nl); } while ($nextpos > $firstpos); return $haystack; } //$needle = string to find //$repl = string to replace needle //$haystack = string to do replacing in //$interval = number of needles in loop //$first=1 = first occurrence of needle to replace (defaults to first) //$pos=0 = position in haystack string to start from (defaults to first) //replace every nth occurrence of $needle with $repl, starting from any position, in a single-level array function arr_replace_int($needle, $repl, $arr, $interval, $first=1, $pos=0, $glue='|+|') { if (!is_array($arr)) return $arr; foreach($arr as $key=>$value){ if (is_array($arr[$key])) return $arr; } $haystack = implode($glue, $arr); $haystack = str_replace_int($needle, $repl, $haystack, $interval, $first, $pos); $tarr = explode($glue, $haystack); $i = 0; foreach($arr as $key=>$value){ $arr[$key] = $tarr[$i]; $i++; } return $arr; } ?> If $arr is not an array, or a multilevel array, it is returned unchanged. ---- Server IP: 69.147.83.197 Probable Submitter: 184.61.88.91 ---- Manual Page -- http://www.php.net/manual/en/function.strpos.php Edit -- https://master.php.net/note/edit/106407 Del: integrated -- https://master.php.net/note/delete/106407/integrated Del: useless -- https://master.php.net/note/delete/106407/useless Del: bad code -- https://master.php.net/note/delete/106407/bad+code Del: spam -- https://master.php.net/note/delete/106407/spam Del: non-english -- https://master.php.net/note/delete/106407/non-english Del: in docs -- https://master.php.net/note/delete/106407/in+docs Del: other reasons-- https://master.php.net/note/delete/106407 Reject -- https://master.php.net/note/reject/106407 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184322) next »