i have a php variable that i have to display the length of as a 3 digit number that is right aligned [closed]

StackOverflow https://stackoverflow.com/questions/15689464

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.

有帮助吗?

解决方案

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

其他提示

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_"
?>

The printf version:

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

40 chars right justified

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top