tmyonline Posted February 16, 2008 Share Posted February 16, 2008 Dear all, Suppose I have a string "xyz". This string has length strlen("xyz") = 3. Is there a way to determine the length of this string in pixel instead ? In other words, how do I convert the value of 3 into pixel ? Thanks. Link to comment https://forums.phpfreaks.com/topic/91348-how-to-calculate-the-pixel-length-of-a-string/ Share on other sites More sharing options...
Barand Posted February 16, 2008 Share Posted February 16, 2008 if it's a GD font use $textwidth = 3 * imagefontwidth('x'); If it's a truetype font, get the coords of it's bounding-box with imagettfbbox() Link to comment https://forums.phpfreaks.com/topic/91348-how-to-calculate-the-pixel-length-of-a-string/#findComment-468110 Share on other sites More sharing options...
cooldude832 Posted February 16, 2008 Share Posted February 16, 2008 can I ask what your intentions are for this? if you are looking for dynamic css look at the EM unit for font sizes its very nice Link to comment https://forums.phpfreaks.com/topic/91348-how-to-calculate-the-pixel-length-of-a-string/#findComment-468124 Share on other sites More sharing options...
tmyonline Posted February 16, 2008 Author Share Posted February 16, 2008 Guys, what I want to do is this. I'm designing a menu as shown below: Chicken wings (3 pieces) ..............................................$4 Chicken wing, thigh, and breast .....................................$5.95 Chicken combo ...........................................................$12.75 I need to fill in the dots [.....................] between the menu item and the price in such a way that the prices align to the left. Because the menu items are of different lengths, I have to calculate their lengths, convert them into pixels, and subtract them from a fixed number to determine the lengths for the dots. I tried Barand's suggestion but it didn't work. But, thanks Barand, I appreciate your help. Hope to hear from others on this. Thanks. Link to comment https://forums.phpfreaks.com/topic/91348-how-to-calculate-the-pixel-length-of-a-string/#findComment-468134 Share on other sites More sharing options...
roopurt18 Posted February 16, 2008 Share Posted February 16, 2008 Look into sprintf(). You can force a string to a certain length and choose the padding string (such as a dot in your case). The solution won't be immediately apparent but you should be able to get it with some tinkering. Link to comment https://forums.phpfreaks.com/topic/91348-how-to-calculate-the-pixel-length-of-a-string/#findComment-468232 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.