Page 1 of 1

Using sprintf to right justify a string

Posted: Tue Aug 25, 2009 9:09 pm
by ocpaul20
Hi all,
I am having problems getting the sprintf to work correctly with left and right formatting

For example my string is 16 characters "Yellow Submarine"

1) What I want to display is "low Submarine" using sprintf formatting and I do not seem to be able to do it.

My code so far......

Code: Select all

$str = "Yellow Submarine";
$fmt = "%-13.13s"; // attempt at right-justification but with a cutoff of 13 characters
echo "<BR>Format [".htmlentities($fmt)."] = ".sprintf('['.$fmt.']',$str);
 
 
the output from the code is:
Format [%-13.13s] = [Yellow Submar]

Can someone please help me to right justify it?
Thanks.

Re: Using sprintf to right justify a string

Posted: Tue Aug 25, 2009 10:04 pm
by requinix
You just want the last 13, right? Any reason for sprintf?

Code: Select all

echo substr($text, -13);