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