Forum Moderators: coopster

Message Too Old, No Replies

PHP printf padding with link

Syntax please

         

davez1000

4:47 pm on May 1, 2003 (gmt 0)

10+ Year Member



Can someone please help me on this,

I want a link inside a printf statement, with padding, so


printf("%-20s%20.2f\n", $key, $val);

What syntax do I use to make the first

%s
a link, but also keep the
-20
padding? I have tried a few things, but it also counts the characters in the href tag itself, and throws the right column out, I gather it's because of the
<pre>
tags. I'm fairly new with printf, so maybe I'm overlooking something completely.

Thanks for any help.

daisho

12:26 am on May 2, 2003 (gmt 0)

10+ Year Member



I think you will have issues trying to have a link inside a pre tag since the prupose of the pre tag is to display the raw text without parsing it.

daisho.

bonanza

2:39 am on May 2, 2003 (gmt 0)



Daisho, do you mean that html tags won't be parsed inside pre tags? I don't think that's the case.

The pre tag is to indicate that the text is preformatted. So while in regularly formatted HTML, extra whitespaces, carraige returns, etc. are ignored, inside pre tags they're left intact.

HTML is still parsed, so you can have a link, bolded text, etc. within pre tags.

bonanza

3:07 am on May 2, 2003 (gmt 0)



davez1000,

The problem that you're seeing is that the printf doesn't know that you've got non-displayed characters (the html) in your text. So it's formatting as if all characters would be displayed.

You're going to need to be a little tricky to get the formatting to be what you want.

From your description, I'm assuming that $key is some html with an <a> tag in it and that $val is some number.

In order to do the formatting properly, you need to know the length of the visible text that's in the link.

There may be a better way to do it, but I think this gets the affect that you want:

I've added another variable $key_text that contains just the text of the link so I can find out how long that is.

Then I count the length of that text and subtract it from 20 (your pad amount) to calculate the amount of space needed.

Then I use that value to put some variable padding in the printf.

So, no matter how long the text of the link, the number will always show up in the same column.


<pre>
123456789¦123456789¦123456789¦123456789¦213456789¦123456789¦
<?php
$key_text = "link text";
$key = "<a href=\"http://www.test.com\">$key_text</a>";
$val = 99.9999;
$pad = 20 - strlen($key_text);
printf("%s%-" . $pad . "s%20.2f\n", $key, "", $val);
?>
</pre>

Experiment with this to see if it gets you closer to what you want.