note 105990 modified in function.base-convert by danbrown

From: Date: Fri, 30 Sep 2011 21:43:28 +0000
Subject: note 105990 modified in function.base-convert by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-183575@lists.php.net to get a copy of this message
A simple function to convert an int to any base. <?php function intToAlphaBaseN($n,$baseArray) { $l=count($baseArray); $s = ''; for ($i = 1; $n >= 0 && $i < 10; $i++) { $s = $baseArray[($n % pow($l, $i) / pow($l, $i - 1))].$s; $n -= pow($l, $i); } return $s; } $alpha=array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'); $r=rand(0, 99999999999); echo(intToAlphaBaseN($r,$alpha)); ?> --was-- A simple function to convert an int to any base. function intToAlphaBaseN($n,$baseArray) { $l=count($baseArray); $s = ''; for ($i = 1; $n >= 0 && $i < 10; $i++) { $s = $baseArray[($n % pow($l, $i) / pow($l, $i - 1))].$s; $n -= pow($l, $i); } return $s; } $alpha=array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'); $r=rand(0, 99999999999); echo(intToAlphaBaseN($r,$alpha)); http://php.net/manual/en/function.base-convert.php

« previous php.notes (#183575) next »