how to convert int to string with fixed length - php

I need a function that will be convert an int value to string with fixed length.
For example :
5 -> '000005', 5245 -> '005245' ..
If the number is bigger than 999999 it stays as it is:
2548562 -> '2548562'
Is there PHP function to do that, or do I need to create a custom function?

What you want is this
$value = str_pad($value, 6, '0', STR_PAD_LEFT);
This will pad your integer value with 0's as a string. If there are more than 6 digits, the integer will simply stay as is.
Example:
<?php
$value = 5;
$value = str_pad($value, 6, '0', STR_PAD_LEFT);
print($value . "\xA");
$value = 2548562;
$value = str_pad($value, 6, '0', STR_PAD_LEFT);
print($value);
?>
Output:
000005
2548562

You can use this
$resultString = str_pad($value, length , '0' , STR_PAD_LEFT);

Related

Yii-2 Unable to format a number with leading zeros in PHP

Using this solution. I am trying to format a number with leading zero's. The length is 12 digits. The starting is 00000000001 and if the number is 10 then 000000000010 and so on. I have tried below
OGP-<?php $model=$dataProvider->getModels()[0];
str_pad($model['OGP_Serial_No'], 12, '0', STR_PAD_LEFT)?>
But it's giving me empty result OGP-.
How can I achieve this?
Any help would be highly appreciated.
Try this
<?php
if(isset($dataProvider->getModels()[0]) && $dataProvider->getModels()[0] != array()){
$model = $dataProvider->getModels()[0];
$OGP_Serial_No = $model['OGP_Serial_No'];
$myNumber = "OGP-".str_pad($OGP_Serial_No, 12, '0', STR_PAD_LEFT);
}else{
$myNumber = "OGP-".str_pad(1, 12, '0', STR_PAD_LEFT);
}
echo $myNumber;
?>
Try check the real content eg: using var_dump()
<?php
$models = $dataProvider->getModels(); [0];
$myString = 'OGP-' . str_pad( $models[0]['OGP_Serial_No'] , 12, '0', STR_PAD_LEFT);
var_dump($myString);
?>

How format number value in php?

I want format number as style '00000' ex:
$a=1 => $a2="00001"
$b=23 => $b2="00023"
How format number in php?
You should use str_pad : https://www.php.net/manual/en/function.str-pad.php
$formattedValue = str_pad($value, 6, "0", STR_PAD_LEFT);
sprintf:
$formattedValue = sprintf('%05s', $value);
0 is the character to pad 5 times. s specifies that it's a string.
use the str_pad function:-
$a2 = str_pad($a, 5, "0", STR_PAD_LEFT);

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.

Strange behavior with PHP's str_pad() and numbers

$caseid is (usually) a 5-digit number (it could be lower). I want a leading 0 so that the number is 6 digits long:
<?php
$caseid=12345;
echo str_pad(strval($caseid), 6-strlen(strval($caseid)), '0', STR_PAD_LEFT); ?>
This doesn't work as expected (displays the same number). Instead if I add 5 to the second argument of str_pad, it works.
<?php echo str_pad(strval($caseid), 11-strlen(strval($caseid)), '0', STR_PAD_LEFT); ?>
It works. What is the error here?
You don't need to compute the difference you just put the total characters you want it to be.
str_pad(strval($caseid), 6, '0', STR_PAD_LEFT);
I was once bothered by str_pad()s behavior too. I got tired of it, and returned to old stable sprintf().
<?php
header('Content-Type: text/plain');
$test = [
1,
12,
123,
1234,
12345,
123456
];
foreach($test as $n){
echo sprintf('%06d' . PHP_EOL, $n);
}
?>
Result:
000001
000012
000123
001234
012345
123456
Maybe, it is not an answer, but I hope it might help somebody.
See the function prototype:
string str_pad (
string $input ,
int $pad_length
[, string $pad_string = " "
[, int $pad_type = STR_PAD_RIGHT ]]
)
Pad a string to a certain length with another string, $pad_length means the length you want.
str_pad in php.net

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

Categories