note 74086 deleted from language.operators.increment by bjori
| From: | bjori@php.net | Date: | Sat, 21 Apr 2007 08:09:09 +0000 |
| Subject: | note 74086 deleted from language.operators.increment by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125738@lists.php.net to get a copy of this message | ||
Note Submitter: php dot net at kender dot nospam dot org
----
this can probably be more easilly done with case examples but i had a case where i needed to come up
with the ability to add an turn 11 digits into a field where only 10 would fit (database
restrictions) so had to convert the basic numrical into a more unique feature
this will go the normal 0-9 then a-z, then A-Z (so the first 10 turned into 62 possibilities)
using this function,
a 1 digit database field now can hold 62 records, isntead of 10
a 2 digit database field now can hold 3843 records
a 3 digit field can now hold... im sure you see its exponental
<?
$number = '1zZorl4'; // starting number
$newnum = incremental($number);
echo $number;
echo "<br />";
echo $newnum; // this just to show that its working
function incremental($number) {
$number = str_pad($number, 10, '0', STR_PAD_LEFT); // adds leading zeros
$ones = substr($number, 9, 1);
$tens = substr($number, 8, 1);
$hund = substr($number, 7, 1);
$thou = substr($number, 6, 1);
$ttho = substr($number, 5, 1);
$htho = substr($number, 4, 1);
$mils = substr($number, 3, 1);
$tmil = substr($number, 2, 1);
$hmil = substr($number, 1, 1);
$bils = substr($number, 0, 1); // breaks down to placement
if ($ones == '9') {
$ones = 'a';
} else if ($ones == 'z') {
$ones = 'A';
} else if ($ones == 'Z') {
$ones = '0';
if ($tens == '9') {
$tens = 'a';
} else if ($tens == 'z') {
$tens = 'A';
} else if ($tens == 'Z') {
$tens = '0';
if ($hund == '9') {
$hund = 'a';
} else if ($hund == 'z') {
$hund = 'A';
} else if ($hund == 'Z') {
$hund = '0';
if ($thou == '9') {
$thou = 'a';
} else if ($thou == 'z') {
$thou = 'A';
} else if ($thou == 'Z') {
$thou = '0';
if ($ttho == '9') {
$ttho = 'a';
} else if ($ttho == 'z') {
$ttho = 'A';
} else if ($ttho == 'Z') {
$ttho = '0';
if ($htho == '9') {
$htho = 'a';
} else if ($htho == 'z') {
$htho = 'A';
} else if ($ttho == 'Z') {
$ttho = '0';
if ($mils == '9') {
$mils = 'a';
} else if ($mils == 'z') {
$mils = 'A';
} else if ($mils == 'Z') {
$mils = '0';
if ($tmil == '9') {
$tmil = 'a';
} else if ($tmil == 'z') {
$tmil = 'A';
} else if ($tmil == 'Z') {
$tmil = '0';
if ($hmil == '9') {
$hmil = 'a';
} else if ($hmil == 'z') {
$hmil = 'A';
} else if ($hmil == 'Z') {
$hmil = '0';
if ($bils == '9') {
$bils = 'a';
} else if ($bils == 'z') {
$bils = 'A';
} else {
$bils++;
}
} else {
$hmil++;
}
} else {
$tmil++;
}
} else {
$mils++;
}
} else {
$htho++;
}
} else {
$ttho++;
}
} else {
$thou++;
}
} else {
$hund++;
}
} else {
$tens++;
}
} else {
$ones++;
}
$number = $bils . $hmil . $tmil . $mils . $htho . $ttho . $thou . $hund . $tens . $ones;
$number = ltrim($number, '0'); // remove padding
return $number;
}
?>