note 102210 deleted from function.range by danbrown

From: Date: Wed, 02 Feb 2011 23:22:08 +0000
Subject: note 102210 deleted from function.range by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-176605@lists.php.net to get a copy of this message
Note Submitter: creator at mindcreations dot com ---- /** * * Represents a sorted integer array as sets * Sets are summarized and represented as a single string that is easily readable for human beings * * @param array $v - Sorted integer array you wish to summarize (it works even with number represented as strings) * @param int $offset - Number of digits to ignore starting from the beginning of the array * @param int $step - Step size to summarize * @param bool $compress - Option to represent contiguous sets with a compressed notation: only digits that do not differ between the edges are included * @param int $trunc_left - Truncates the leftmost N digits in the final representation, for both edges of each set * @param string $seq_sep - Separator for non contiguous sets * @param string $range_sep - Separator for contiguous sets * @return string Resulting summarized sets * * @example $a = array(3, 4, 7, 8, 9, 10, 12); * print_r(range_compact($a)); * OUTPUT: 3-4, 7-10, 12 * @example $a = array(30, 40, 70, 80, 90, 100, 120); * print_r(range_compact($a, 0, 10)); * OUTPUT: 30-40, 70-100, 120 * @example $a = array(3906123456780, 3906123456781, 3906123456782, 3906123456785, 3906123456790, 3906123456791, 3906123456792, 3906123456793, 3906123456794, 3906123456795, 3906123456796, 3906123456797, 3906123456798, 3906123456799, 3906123456800); * print_r(range_compact($a, 0, 1, true, 2)); * OUTPUT: 06123456780-2, 06123456785, 06123456790-800 * print_r(range_compact($a, 0, 1, false, 2)); * OUTPUT: 06123456780-06123456782, 06123456785, 06123456790-06123456800 * print_r(range_compact($a, 0, 1)); * OUTPUT: 3906123456780-3906123456782, 3906123456785, 3906123456790-3906123456800 */ function range_compact($v, $offset = 0, $step = 1, $compress = false, $trunc_left = 0, $seq_sep = ", ", $range_sep = "-") { $lun = count($v) + 1; $firstseq = $offset; $lastseq = $firstseq; for($i = $offset + 1; $i < $lun; $i++) { if($v[$i - 1] == $v[$i] - $step) { $lastseq = $i; } else { $sysc = $sysc . substr($v[$firstseq], $trunc_left); if($lastseq > $firstseq) { if($compress == false) { $sysc = $sysc . $range_sep . substr($v[$lastseq], $trunc_left); } else { $lastelem = substr($v[$lastseq], strlen(common_prefix($v[$firstseq],$v[$lastseq]))); $sysc = $sysc . $range_sep . $lastelem; } } if($i < $lun - 1) $sysc = $sysc . $seq_sep; $firstseq = $i; $lastseq = $firstseq; } } if($lastseq > $firstseq) { return $sysc . substr($v[$firstseq], $trunc_left) . $range_sep . substr($v[$lun], $trunc_left); } else { return $sysc . substr($v[$lun], $trunc_left); } } /** * * Returns the common part of two string starting from left * @param string $s1 - First string * @param string $s2 - Second string * @return string Common part of the two strings starting from left * @example common_prefix("abc", "ab") = "ab" */ function common_prefix($s1, $s2) { $s1 = strval($s1); $s2 = strval($s2); $n = min(strlen($s1), strlen($s2)); $i = 0; while ($i < $n && $s1{$i} === $s2{$i}) { $i++; } return substr($s1, 0, $i); }

« previous php.notes (#176605) next »