Using sprintf to right justify a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ocpaul20
Forum Newbie
Posts: 12
Joined: Thu Jul 05, 2007 3:53 am

Using sprintf to right justify a string

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using sprintf to right justify a string

Post by requinix »

You just want the last 13, right? Any reason for sprintf?

Code: Select all

echo substr($text, -13);
Post Reply