-4

this is a really noob question I guess, but I am very new to php and have no idea how to even begin this one, basically I have a set of $vars that I have to display the length of as a 3 digit number that is right justified.

Any pointers as to how to do this?

Many thanks for all your help.

Eric Edward
  • 23
  • 1
  • 1
  • 2

3 Answers3

0

Your answer lies within PHP documentation: http://php.net/manual/en/function.str-pad.php

From their example:

<?php
$input = "Alien";
echo str_pad($input, 10);                      // produces "Alien     "
echo str_pad($input, 10, "-=", STR_PAD_LEFT);  // produces "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH);   // produces "__Alien___"
echo str_pad($input, 6 , "___");               // produces "Alien_"
?>
Daedalus
  • 1,667
  • 10
  • 12
0

This should take care of what you just described in your comment:

echo str_pad(strlen("hello"), 3 , "0", STR_PAD_LEFT);

This will return

005
Kermit
  • 33,827
  • 13
  • 85
  • 121
  • Thanks, but i need the value the total of characters in a variable, so $var = "hello" // 5 characters I need to display this as 005 – Eric Edward Mar 28 '13 at 18:55
  • @EricEdward And what if it's more than 999 characters? – Kermit Mar 28 '13 at 19:09
  • The variables are fixed in this case, so they will never go over 999, however I do see your point and is something worth considering in the future. – Eric Edward Mar 29 '13 at 06:57
0

The printf version:

printf("%40d", 123); //                                      123

40 chars right justified

Mike B
  • 31,886
  • 13
  • 87
  • 111