Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP sprintf space padding

Tags:

php

padding

space

I have the following line in my code which displays my output in 6 characters with leading zeros.

$formatted_value = sprintf("%06d", $phpPartHrsMls);

I want to replace the leading zeros with spaces. Have tried all the examples found by searching this site and others and cannot figure it out.

Here are some I have tried:

$formatted_value = sprintf("%6s", $phpPartHrsMls);

$formatted_value = printf("[%6s]\n",    $phpPartHrsMls); // right-justification with spaces
like image 376
user2883088 Avatar asked Jun 20 '14 23:06

user2883088


1 Answers

In the browser, spaces will always be collapsed.

Try:

<pre><?php echo $formatted_value; ?></pre>

And once you're satisfied with that, take a look at the CSS white-space:pre-wrap - a very useful property!

like image 175
Niet the Dark Absol Avatar answered Sep 28 '22 03:09

Niet the Dark Absol