In: PHP
August 31st, 2010.In order to format output, sometimes you need to add leading characters. PHP has a str_pad function that lets add leading or trailing characters to achieve string length.
In following snippet, you can see simple example of usage.
1 2 3 4 5 6 | function add_leading_zeros($input, $str_length) { return str_pad($input, $str_length, '0', STR_PAD_LEFT); } //example of usage (this will echo "0013") echo add_leading_zeros('13', 4); |