PHP str_pad and strlen within preg_replace - php

I'm attempting to output a certain number of zeros depending on the number of digits. My code doesn't output what I want.
$x = '12345';
$y = preg_replace('/(\d+)/', str_pad('',(12-strlen("$1")),0), $x);
echo "y = $y";
# expected output: y = 0000000 (7 zeros)
# output: y = 0000000000 (10 zeros)

Like dev-null-dweller said in the comments you should use preg_replace_callback():
// This requires PHP 5.3+
$x = '12345';
$y = preg_replace_callback('/\d+/', function($m){
return(str_pad('', 12 - strlen($m[0]), 0));
}, $x);
echo "y = $y";

I did this which works:
<?php
$x = '12345';
$y = str_pad(preg_replace('/(\d+)/', "0", $x), 12 - strlen($x), "0", STR_PAD_LEFT);
echo "y = $y";
There is also this regular expression version too:
$y = str_pad(preg_replace('/\d/', "0", $x), 12 - strlen($x), "0", STR_PAD_LEFT);
There is also this, if you wan't the final output to look like: 0012345
$y = str_pad($x, 7, "0", STR_PAD_LEFT);

Related

PHP Calculation with 0 in front of variable value

How to allow PHP calculation 0 appears if example :
$a = "00001";
$b = "00005";
$total = $a + $b;
Expected result is : 00006
Including and keep 0000 appears.
I tried but the result always : 6
Just do normal math and then add the zeros back in front at the end:
$a = "00001";
$b = "00005";
$total = $a + $b;
$total = sprintf("%05d", $total );
or
$total = str_pad($total , 5, "0", STR_PAD_LEFT);
You might want to try and prepend the needed 0's
<?php
$total = 6;
$total_padded = sprintf("%05s", $num);
echo $num_padded; // returns 00006
?>
I haven't tested it but it should work :)
<?php
str_pad($total, 5, '0', STR_PAD_LEFT);
?>
I assume that the value is integer so you should use intval. If it is float use floatval
$a = "00001";
$b = "00005";
print intval($a);
print intval($b);
print intval($a)+intval($b);
than add the leading zeros
<?
str_pad($num, 5 , '0', STR_PAD_LEFT);
?>

PHP string Formatting

Using PHP I want to format a number(7) like in 6 digits 000007
I had done this via printf Function but wanted to store it in a variable how can I do so.
Below mentioned code not working, giving output as :000007 and printing it on screen:
$gensubjectid=printf("%06d\n", $origionalsubjectid);
Suggest.
Use sprintf - it's identical to printf but it returns the formatted string.
You can use: spirntf
$unformattedNumber = 7;
$formattedNumber = sprintf('%06d', $unformattedNumber);
Or you can try this (str_pad):
$input = 7;
str_pad($input, 6, "0", STR_PAD_LEFT);
Use str_pad().
$invID = str_pad($invID, 10, '0', STR_PAD_LEFT);
Or
Use sprintf: http://php.net/function.sprintf
$number = 51;
$number = sprintf('%10d',$number);
print $number;
// outputs 0000000051
$number = 8051;
$number = sprintf('%10d',$number);
print $number;
// outputs 0000008051

PHP set int length

Alright simple question
I have integriters like
5
188
4634
And they all need to be formated to
0000000005
0000000188
0000004634
It can become a string that doesnt matter.
sprintf is the function for that:
$num = sprintf("%010d", $num);
str_pad
echo str_pad($str, 10, "0",STR_PAD_LEFT);
<?php
#how many chars will be in the string
$filltotal = 10;
$number = 5;
#with str_pad function the zeros will be added
echo str_pad($number, $fill, '0', STR_PAD_LEFT);
// The result: 0000000005
Alternative is str_pad:
echo str_pad($num, 10, "0", STR_PAD_LEFT);
you can achieve by this.
$val = 12;
for($i=0;$i<(10 - count($val));$i++)
{
$str .= '0';
}
$final_val = $str.$val;

Is there native function to automatically put specific number of zeros based on actual and maximal value?

Is there any native PHP function which would allow me to do this?
$number = functionName(1, 9999); // $number == 0001
$number = functionName(10, 99); // $number == 10
$number = functionName(10, 999); // $number == 010
If there isn't native function, is there any user-based function?
echo str_pad($value, strlen($max), "0", STR_PAD_LEFT);
for example
$value = 10;
$max = 999;
echo str_pad($value, strlen($max), "0", STR_PAD_LEFT);
// returns 010
See http://ideone.com/KPBV2 and str_pad()
use str_pad its easy! here's the doc
http://php.net/manual/en/function.str-pad.php
Not exactly, but you can write one easily using str_pad and its pad_type of STR_PAD_LEFT.
There sure is: str_pad()
<?php
echo str_pad(33, 5, "0", STR_PAD_LEFT);
echo str_pad(1, 5, "0", STR_PAD_LEFT);
echo str_pad(1258, 5, "0", STR_PAD_LEFT);
echo str_pad(89965, 5, "0", STR_PAD_LEFT);
?>

how do i use 4 digit numbers

I need to count from:
0-9999
In PHP, how can i make the format:
0000-9999
So that the output is:
0000,0001,0002,....
thanks!
$num = 9;
$paddedNum = sprintf("%04d", $num);
echo $paddedNum; // prints 0009
$number = 9;
echo str_pad($number, 4, '0', STR_PAD_LEFT); // output: 0009

Categories