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