I want to generate a serial key with this pattern :
DA-7470-20762
and this is what I have done so far :
$code = strtoupper(md5(uniqid(microtime(true), true)));
$uuid = sprintf("%02s-%04o-%04o",
$code[0].$code[1],
$code[2].$code[3].$code[4].$code[5],
$code[6].$code[7].$code[8].$code[9]
);
But I want to prevent serial keys like this :
AB-0000-0001
I need to remove all 0's, because they are so predictive.
Is there a way to set range for octal sprintf like [1-9]?
I'd probably go with something along these lines:
printf('%2s-%4o-%4o',
chr(mt_rand(65, 70)) . chr(mt_rand(65, 70)),
mt_rand(01000, 07777),
mt_rand(01000, 07777));
If you're just looking for a way to minimise zeros, you could opt for a pre-serial-generation modification along the lines of the following pseudo-code:
for each idx in 2, 4, 6 and 8:
if code[idx] == "0":
code [idx] = randomNumBetween("1","7")
Related
I have a string number as below. When i add 1 to it, it changes to a scientific representation instead of a real number and also doesnot increment to the next number. Any reason why its not incrementing?
I want 4600004722057511 to become 4600004722057512
Thanks
Number is 4600004722057501
After adding 1 to it it becomes 4.6000047220575E+15
$val = $list[0]->getNextNumber();
$list[0]->setNextNumber($val+1);
use bcmath for high numbers
$val = $list[0]->getNextNumber();
$val = bcadd( $val, 1 ); // here you add 1 to a very high number
$list[0]->setNextNumber($val);
You can try with add “(int)” your code
$val = (int)$list[0]->getNextNumber();
$list[0]->setNextNumber($val+1);
Try this:
$val = (float)$list[0]->getNextNumber();
$list[0]->setNextNumber($val+1);
since PHP treat number as number, it won't be a problem, but if you want to display its value somewhere, cast it to string
$val = $list[0]->getNextNumber()+1;
$val = (string)$val
Try This
$val =$list[0]->getNextNumber();
$val=++$val;
$list[0]->setNextNumber($val);
If it not works, show your full code (might be a problem in function code).
I would like to post a value to my MySQL database in this format;
01, 02, 03...
011, 012, 013...
0101, 0102, 0103,
etc.
(with a 0 before each value).
If I do it like this "01+1" I (understandable) get the value 2 and not "02".
is there a default php function that let's me set the default amount of characters or is there an other option?
I'm using laravel combined with eloquent and some own functions like so;
$lastSKU = $data->GetData('get', 'products', 'filter')->max('SKU');
$newValue = $lastSKU+1;
Thanks in advance.
It sounds like you won't know how the format will be, if there is always a 0 in front it is easy enough to do
$value = '0'. ( $value + 1 );
PHP will automatically convert it to a number between the brackets and by adding a string to it it will become a string again.
Now if you do not know the the length or the padding in advance but you do need to keep the current padding I would suggest something like
$value = str_pad(($value + 1), strlen($value), '0', STR_PAD_LEFT);
Edit, str_pad is a bit slow these days, sprintf can do the same trick but much faster
$value = sprintf("%'.0" . strlen($value) . "d\n", ($value +1));
I'm trying to format specific numbers up to 8 decimals by deleting unnecessary zeros.
My actual code is:
rtrim(sprintf("%.8f", $upto_eight_decimals), '0')
It actually prevents to format a number as 0.00012 into 1.2E-4 or 0.00012000
However, with numbers integer such as 1 it gets converted into 1. but this point is not my expected result (I know because of rtrim deleting all zeros).
UPDATE: rtrim(rtrim(sprintf("%.8f", $upto_eight_decimals), '0'), '.') it looks like working
You can do it this way, Just use number_format:
$upto_eight_decimals = "0.0001200";
$out = number_format((float)$upto_eight_decimals, 8, '.', '');
echo preg_replace("/\.?0*$/",'',$out);
or
echo $out + 0;
This function returns a string.
This will work for you, let me know is it work or not.
I doubt if it is possible but I'm looking for the following:
E.g. $number's value is 1, can I get the next number, in this case 2, to be the value of another variable, e.g. $newnumber?
I prefer to do this in SQLite, so the numbers are stored in a database.
Try: $newnumber = ((int) ($number)) + 1, if this is for a primary key though just set the column to auto increment
$newnumber=$number+1;
I think it can't get more simpler than that.
Without knowing the larger scope of what you're trying to accomplish, my suggestion would be to use the increment operator in PHP.
Original answer:
Something like:
$number = 1;
$newNumber = $number++;
Correct answer (above gives wrong result):
$number = 1;
$number++;
$newNumber = $number;
From there you can do whatever you want with the second variable.
how does the "tiny url" sites get so tiny ID url ?
i mean this : blabla.com/JH7
how can i get to such result? a functionality that is like md5 that does not repeat it self.
thanks in advance!
For example you can simply iterate trough string:
php > $str = 'aaa';
php > $str++;
php > echo $str;
aab
The another option is to prepare function which will generate random strings containing of a-zA-Z0-9 and than generate few millions of them into db (so you could just use them when needed) or do it in loop:
while( 1){
$rand = randomString();
if( isUnique( $rand)){
break;
}
}
Make a database table with the columns short_url and url.
Start by inserting the record a, example.com.
Increment short_url with each new entry (b, c, ..., a1 ...).
That's basically how these services work.
They use base36 encoding to convert an integer to a compact string like that.
Using PHP:
<?php
$id = 18367;
$base36 = base_convert($id, 10, 36); // convert to base36 "e67"
$base10 = base_convert($base36, 36, 10); // "e67" back to base 10, $id
As stated by deceze, base62 is also suitable which gives you a character set of a-zA-Z0-9 instead of just a-z0-9 like base36 does.