Jump to content

[SOLVED] sprintf variable padding


xiaix

Recommended Posts

This code:

if ($data1["e"] > 0) {	$part .= sprintf("<strong>%s %d %s</strong>", $data1["e"] > 0 ? "+":"-", $data1["e"], $defType["e"]); if ($howManyArmorDef > ($e1 += 1)) { $part .= "<BR />"; }}

 

Displays this:

+54 Melee Piercing Defense

 

 

Fine, but if I use this code (notice the %5s):

if ($data1["e"] > 0) {	$part .= sprintf("<strong>%s %d %5s</strong>", $data1["e"] > 0 ? "+":"-", $data1["e"], $defType["e"]); if ($howManyArmorDef > ($e1 += 1)) { $part .= "<BR />"; }}

 

I still get this:

+54 Melee Piercing Defense

 

But I should get this:

+54      Melee Piercing Defense

 

 

What gives?  What am I doing wrong?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/171967-solved-sprintf-variable-padding/
Share on other sites

*update*

 

<pre> tags got messy, and after much trying to get things to pad correctly, I gave up and opted for table/row/columns to handle my text padding/justification issues.  Everything works great now.

 

Example:

		// Let's list armor defenses
		if ($howManyArmorDef>0) {  
			$part .= sprintf("<BR /><strong>Defenses</strong><BR />");
			$part .= sprintf("<table>");
			if ($data1["e"] > 0) {	$part .= sprintf("<tr><td>%+d</td><td>[%s]</td></tr>", $data1["e"], $defType["e"]); }
			if ($data1["f"] > 0) {	$part .= sprintf("<tr><td>%+d</td><td>[%s]</td></tr>", $data1["f"], $defType["f"]); }
			if ($data1["g"] > 0) {	$part .= sprintf("<tr><td>%+d</td><td>[%s]</td></tr>", $data1["g"], $defType["g"]); }
			if ($data1["h"] > 0) {	$part .= sprintf("<tr><td>%+d</td><td>[%s]</td></tr>", $data1["h"], $defType["h"]); }
			if ($data1["i"] > 0) {	$part .= sprintf("<tr><td>%+d</td><td>[%s]</td></tr>", $data1["i"], $defType["i"]); }
			if ($data1["j"] > 0) {	$part .= sprintf("<tr><td>%+d</td><td>[%s]</td></tr>", $data1["j"], $defType["j"]); }
			$part .= sprintf("</table>");
		}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.