tags:

views:

185

answers:

7

Hi,

I have a list of strings that I would like to display in a HTML select object. The strings look something like :

id - name - description

I would like the fields to align however. In PHP I'm using

sprintf ("%4s%10s%20s", $id, $name, $description);

which works fine. The problem is the multiple spaces is compacted to 1 space in the select list. I tried using the pre and white-space CSS properties of the select box, but it has no effect. Any suggestions?

+3  A: 

Use CSS to set the font of the options to a monospaced font. Use   instead of spaces (you can use str_pad() instead of sprintf()).

http://us3.php.net/str_pad

Scott Saunders
I was using monospaced fonts. Your solution works too, but I preferred the single str_replace to 3 str_pad's.
Marius
A: 

Replace spaces with   HTML entity.

Goran Jurić
A: 

You may have to replace the spaces with   to make this work correctly; though part of me thinks there's a much better to do this but my HTML-fu is weak...

Austin Salonen
A: 

Wrap it in the html "code" tag.

Brent Arias
A: 

Replace the multiple spaces following each other with non breaking spaces:

 
Pim Jager
A: 

Convert spaces to  ?

Try to use   (Non breaking space) is that what your looking for?

Lizard
+1  A: 

Hi there, you have to use   instead, they are non-breaking spaces so it won't collapse.

You could do:

str_replace(" ", " ", sprintf("%4s%10s%20s", $id, $name, $description));
sirhc
Arg too late...
sirhc
This works. It seems a little clumsy though, but it works :)
Marius