Right alignment (justification) of a PHP string

In a project the client wanted that I right align numbers in a table that have text-align: left table cells. Great that PHP has a core function named str_pad which helps solving this situation. So in my case I used

echo '<td><span class="white-space-pre">
    ' .  str_pad( $number, 6, ' ', STR_PAD_LEFT ) . '

Then I had to fix the white space issue because it looked like this:

After adding the following CSS rule it moved like I wanted.

.white-space-pre {
    white-space: pre;



Author: Michal Zuber

Full stack developer, biker and rollerblader. Owner and developer at https://nevilleweb.sk/ Co-founded http://neville.sk/ Blog at https://michalzuber.wordpress.com/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s