Is there a way to tell how many pixels certain text will take up?
Printable View
Is there a way to tell how many pixels certain text will take up?
how many pixels? well by depault it is 10px, but I take it that is not what you are after. if you want the same thing like an image on how many pixels it is then that is a good question.
why would you want to know this, for curiousity reasons? :p
I'm guessing he wants to know if there is a PHP equivelent function to VB .TextWidth's. I can see it would be usefull to know if, oh let's say, add text, just to use a nonspecific example, is going to take up more than 500pixels on someone's screen.
Then you could mak necessary adjustments to avoid a flood of backlash when the users of the site are suddenly subjected to the outrageous 72point sized text.
not that I am aware of. the only thing that would come close is strlen(), but that will noly tell you how many characters it has.
but how can it get to 75points if the webmaster doesn't set it that way? the pixel size is controlled by the browser unless you specificly state it in the css.
75points <> 75pixels.
If I set something to Size=5, I have no way of knowing if that's 100px, or 50px. Plus, what may look OK in one rez, looks completely whacko in another.
But then, I was just using it as an example.
well if you new your fonts then you should know what size is 5. it is expained at www.w3.org pretty well. but again the browser can only go so big.
I see where you are coming from and I do know that one size on one pc is different than another.
but again it is the webmaster not the user that sets the font size. os php would have no idea how big it is.
I'll try to provide a specific example later...but I need a table cell's width to be a little more of the width of it's text.
I know that setting it to nothing would do that automatically...I'll try to provide an example later, but I'm busy now.
Thanks for your help so far.
actually that explains a lot and I hav eto tell you that php has little control, if none, over DOM so you would be better off with javascript.
I really don't care what language does it. If you know how to do it in JS, then by all means, clue me in. :)
Here is what I'm trying to do:
Is there any way I can do this? I never know what $text is going to be.Code:<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td width="10">
<table border="0" cellspacing="0" cellpadding="0" style="width: 100%;">
<tr>
<td height="5" bgcolor="#dba842"><img src="space.gif" height="5"></td>
</tr>
</table>
</td>
<td align="center" valign="middle" style="font-size: 9pt; width: 180px;"><b>' . $text . '</b></td>
<td>
<table border="0" cellspacing="0" cellpadding="0" style="width: 100%;">
<tr>
<td height="5" bgcolor="#dba842"><img src="space.gif" height="5"></td>
</tr>
</table>
</td>
</tr>
</table>
http://www.vbshelf.com/layout is an example of it. It's the orange line around "Compiler Companion v1.0"
Any ideas?
you know you could use strlen() on the $text and just add something to it for the width.
ex.
somethnig like that would work.PHP Code:$text_len = strlen("some text"); // 9
$width = $text_len +20;
<td align="center" valign="middle" style="font-size: 9pt; width: {$width}px;">
Oh, well, I get it now..... don't use width in the style.... try this instead....
This will put 5pixels on the left and right of the TD cell.Code:PADDING-RIGHT: 5px; PADDING-LEFT: 5px