Back to examples |
Simple script to create spreadsheet style column headers,
ie AA, BD, D1 etc ( column number to string and visa-versa )
The script allows for a custom character set, so you can use your own characters
or remove the numeric characters
The example shows the 2 functions, one to convert the number to the string,
and one to convert the string back to the number.
|
<?php
// This allows us to use a custom character list
$characters = "abcdefghijklmnopqrstuvwxyz1234567890";
// How many characters are dealing with
$depth = strlen($characters);
function Number2String($nCol) {
global $characters; global $depth;
$sCol="" ; while($nCol >= 0) { $sCol = $characters[$nCol % $depth] .$sCol; $nCol = floor($nCol / $depth) - 1 ; }
// Return the string
return $sCol; }
// This is the inverse function
function String2Number($sCol) { global $characters; global $depth; $nCol = 0; for ($i=0;$i<strlen($sCol);$i++) { $nCol = $nCol * $depth + (strpos($characters,substr($sCol,$i,1))+1) ; } return $nCol; } ?>
|
|