note 119519 added to function.range

From: Date: Tue, 28 Jun 2016 07:58:30 +0000
Subject: note 119519 added to function.range
Groups: php.notes 
Request: Send a blank email to php-notes+get-205825@lists.php.net to get a copy of this message
The function "range" is very useful to get an array of characters as range('C','R') does. At work, I had to extend the function range($a,$b) to work in this special case: with two uppercase strings $a and $b, it should return all the possible strings between $a and $b. This could be used for example to get the excel column indexes. e.g. <?php range('A','AD') ==> array('A','B','C',...,'Z','AA','AB','AC','AD') ?> So I wrote the function getrange($min,$max) that exactly does this. <?php function getcolumnrange($min,$max){ $pointer=strtoupper($min); $output=array(); while(positionalcomparison($pointer,strtoupper($max))<=0){ array_push($output,$pointer); $pointer++; } return $output; } function positionalcomparison($a,$b){ $a1=stringtointvalue($a); $b1=stringtointvalue($b); if($a1>$b1)return 1; else if($a1<$b1)return -1; else return 0; } /* * e.g. A=1 - B=2 - Z=26 - AA=27 - CZ=104 - DA=105 - ZZ=702 - AAA=703 */ function stringtointvalue($str){ $amount=0; $strarra=array_reverse(str_split($str)); for($i=0;$i<strlen($str);$i++){ $amount+=(ord($strarra[$i])-64)*pow(26,$i); } return $amount; } ?> ---- Server IP: 31.14.131.242 Probable Submitter: 2.37.11.19 ---- Manual Page -- http://php.net/manual/en/function.range.php Edit -- https://master.php.net/note/edit/119519 Del: integrated -- https://master.php.net/note/delete/119519/integrated Del: useless -- https://master.php.net/note/delete/119519/useless Del: bad code -- https://master.php.net/note/delete/119519/bad+code Del: spam -- https://master.php.net/note/delete/119519/spam Del: non-english -- https://master.php.net/note/delete/119519/non-english Del: in docs -- https://master.php.net/note/delete/119519/in+docs Del: other reasons-- https://master.php.net/note/delete/119519 Reject -- https://master.php.net/note/reject/119519 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#205825) next »