Incrementing numbers starting from 0000 in php - php

I need a suggestion on a function for a php counter. Is there any function to have numbers with 5 digit as 00001, or 00123… this number should be not random but have to increase the value of a previous field.
If a number is $n=’00001’ there is a function to increase by one and get 00002 and not 2?
Thanks
F.

$n2 = str_pad($n + 1, 5, 0, STR_PAD_LEFT);
Use str_pad() by adding 0s (third parameter) to the left (fourth parameter) of the old number $n incremented by 1 (first parameter) until the length is 5 (second parameter).

$number = 1;
$number++;
echo str_pad($number, 5, "0", STR_PAD_LEFT); //00002

As an alternative, in case you are interested, you can use sprintf to pad this with 0's up to a certain number fairly easily.
$numbers = array(0,1,11,111,1111,11111,11111);
$padded = array();
foreach($numbers as $num)
$padded[] = sprintf('%1$05d', ++$num);
print_r($padded);
PHP almost always has numerous ways to do the same thing. :)

You need the str_pad() function to add leading zeros to your indexes.
$new_index = str_pad($index, 5, "0", STR_PAD_LEFT);
Where $index your incrementing index in circle, $new_index is your index with leading zeros.

Related

How to create an array of numbers prefixed with 0 using str_pad()?

How can I generate an array of numbers using str_pad()?
For example, from 000 to 090 or from 100 to 200.
$n2 = str_pad($n + 1, 3, 0, STR_PAD_LEFT);
The above just adds 1 to $n.
I want to create an array using this method. For example: 000, 001, 002, 003, 004 ...
For my requirements, numbers start at 000 and not 0.
Here's one way, using range. Basically just loop through the range and pad each number as you go. There may be more direct ways but this is a simple approach:
$arr = [];
foreach (range(0, 90) as $n)
{
$arr[] = str_pad($n, 3, 0, STR_PAD_LEFT);
}
var_export($arr);

php math plus don't remove zeros

PLease look my php code. I dont want rmeoving zeros.
<?php
$number = '00154';
$next = $number + 1;
echo $next; // returns 155
?>
But I want to return 00155
Use str_pad() in conjunction with strlen(). strlen() gets the number of digits, and then uses that as the pad length for str_pad().
$next = str_pad($next, strlen($number), '0', STR_PAD_LEFT);
If you always want a fixed length, say 5, then this becomes shorter:
$next = sprintf('%05d', $next);
Demo
You can do that with str_pad if your number length is constant. In your case you have 5 length number. You can get length of number first and you can fill up zeros in result;
<?php
$number = '00154';
$length = strlen($number);
$next = $number + 1;
echo str_pad($next, $length, "0", STR_PAD_LEFT);
?>
Here is a working demo: codepad
00155 and 155 is from a maths perspective EQUAL.
echo str_pad($next, 5, '0', STR_PAD_LEFT);
fills left side with zeroes.
This is because your number is a string. Convert it to a number (integer or float) first, the use math function.

PHP add thousand separators to number and keep leading zeros

I'm trying to add thousand separators to a number using PHP and at the same time keep the leading zeros (It's part of the design of an app that the leading 0s stay so that people can see the number grow towards the set target - a 6 figure number).
My initial attempt was to use str_pad to add the leading zeros if the current number calculated was less than 6 figures long. Then to add the commas I used number_format. The obvious issue is that number_format removes the leading 0s.
$num = 550;
$num_padded = str_pad($num, 6, '0', STR_PAD_LEFT);
echo number_format($num_padded);
So that returns 550 instead of 000,550
Does anyone know of a reliable way to achieve the format I'm looking for?
Thanks!
I have in mind this simple trick:
function padAndFormat($number, $length)
{
if(strlen($number)>=$length)
{
return number_format($number);
}
$number = number_format('1'.str_pad($number, $length-1, '0', STR_PAD_LEFT));
$number[0] = '0';
return $number;
}
//var_dump(padAndFormat('517', 6)); //string(7) "000,517"
another way to do this is to use sprintf
// length can be changed, here is 6
implode(',',str_split(sprintf('%06d', $this->iterator),3));
the result will be :
input
result
4
000,004
400
000,400
23560
023,560
1234567
1,234,567
it can be improved by reading the length, and computing automatically the final length, multiple of 3
I don't need number format but I need that comma! :)
<?php
$zeroes=0;
$num = 550;
$num_padded = str_pad($zeroes, 3, '0', STR_PAD_LEFT);
$num_padded = str_pad($num_padded,strlen($num_padded)+strlen($zeroes), ',', STR_PAD_RIGHT);
$num_padded = str_pad($num_padded,strlen($num_padded)+strlen($num), $num, STR_PAD_RIGHT);
echo $num_padded;
OUTPUT :
000,550
One Liner with PHP string manipulation. Works for any number of digits:
function pad($number, $min_digits){
return strrev(implode(",",str_split(str_pad(strrev($number), $min_digits, "0", STR_PAD_RIGHT),3)));
}
/* Output for 9 digits
0,000,001
0,000,012
0,000,123
0,001,234
0,012,345
0,123,456
1,234,567
12,345,678
123,456,789
/**/

PHP: Using str_pad not working? Why?

I have a string, its content is "24896". Now I want to add some zeros to the left, so I tried:
$test = str_pad($myString, 4, "0", STR_PAD_LEFT);
The result is "24896" again, no zeros added to the left. Am I missing something here?
Thanks!
The second argument to str_pad() takes the full length of the final string; because you're passing 4 and the length of $myString is 5, nothing will happen.
You should choose a width that's at least one bigger than your example value, e.g.:
str_pad($myString, 9, '0', STR_PAD_LEFT);
// "000024896"
Update
This might be obvious, but if you always want 4 zeros in front of whatever $myString is:
'0000' . $myString;
Because you're padding it to length 4, and your string 24896 is 5 characters long, hence it doesn't need to pad anything as it's already more than 4 characters long.
The second parameter in the str_pad function is the new length of the string.
Try
$myString = "24896" ;
$test = str_pad($myString, strlen($myString) + 4, "0", STR_PAD_LEFT);
echo $test;
Output
000024896
Just make the pad first and attach it, presuming you don't know how long it is. No need to calculate the length of the original string:
$x = 4;
$pad = str_pad('', $x, '0');
$test = $pad.$myString;
Or better
$x = 4;
$test = str_pad('', $x, '0').$myString;
The length you specified in the str_function is less than the input string read documentation properly
try this it will work for you
Your String is 5 character
e.g $myString=24896;
Now you want to add 5 zero to the left
then your length will be you string + 5 the actual is 5+5=10;
Now pass this to the function your function will be like this
$test = str_pad($myString, 10, "0", STR_PAD_LEFT);
echo $test;
OUTPUT:
0000024896
how many no zero you want to add? no zeros added because padding length is smaller than your given $myString length.
Please try this one
$number = 24896;
$number = sprintf('%06d', $number);
echo $number;
or use this one
$number = 24896;
$number = str_pad($number, 6, '0', STR_PAD_LEFT);//here 6 is padding length
echo $number;
output
024896

How can I separate a number and get the first two digits in PHP?

How can I separate a number and get the first two digits in PHP?
For example: 1345 -> I want this output=> 13 or 1542 I want 15.
one possibility would be to use substr:
echo substr($mynumber, 0, 2);
EDIT:
please not that, like hakre said, this will break for negative numbers or small numbers with decimal places. his solution is the better one, as he's doing some checks to avoid this.
First of all you need to normalize your number, because not all numbers in PHP consist of digits only. You might be looking for an integer number:
$number = (int) $number;
Problems you can run in here is the range of integer numbers in PHP or rounding issues, see Integers Docs, INF comes to mind as well.
As the number now is an integer, you can use it in string context and extract the first two characters which will be the first two digits if the number is not negative. If the number is negative, the sign needs to be preserved:
$twoDigits = substr($number, 0, $number < 0 ? 3 : 2);
See the Demo.
Shouldn't be too hard? A simple substring should do the trick (you can treat numbers as strings in a loosely typed language like PHP).
See the PHP manual page for the substr() function.
Something like this:
$output = substr($input, 0, 2); //get first two characters (digits)
You can get the string value of your number then get the part you want using
substr.
this should do what you want
$length = 2;
$newstr = substr($string, $lenght);
With strong type-hinting in new version of PHP (> PHP 7.3) you can't use substr on a function if you have integer or float. Yes, you can cast as string but it's not a good solution.
You can divide by some ten factor and recast to int.
$number = 1345;
$mynumber = (int)($number/100);
echo $mynumber;
Display: 13
If you don't want to use substr you can divide your number by 10 until it has 2 digits:
<?php
function foo($i) {
$i = abs((int)$i);
while ($i > 99)
$i = $i / 10;
return $i;
}
will give you first two digits

Categories