What I'm trying to do, is cut a string so it fits in the head of an web page.

I'm making an online test, and I want the test name to be displayed, the test name is dynamic because each test has its own name but the area the test name has to be placed in is static.

I don't have a problem chopping up the string, but I'm having a problem knowing how much to chop off.

Whats happening is that I can only have a max of ~20 'W' (meaning 20 W in a row will fill up the area) but I can have over 40 'i' in a row with room left to spare. I set the chopping to chop it off at 20, but the problem is, all titles look stupid because they're so much smaller than the alloted area.

Is there a way to know, possibly in pixels, the length of a string without going thru each individual character to get its pixel with?

Because if the only way is to examine each letter individually, I know how to do that. But if anyone knows another way, other than guessing :p I would appreciate it. Thanks!

Nimlhûg

04-01-2007, 09:54 PM

PHP probably won't be of much use here. People have different fonts and font sizes, so even if you made a nice little array with the sizes of each letter, it'd still be pretty useless in the end. Some css and possibly a bit of javascript might be of more use, depending on what exactly you want to accomplish.

If all you want to do is stop excess text from appearing in a space, then CSS is definitely your friend.

iLLin

04-01-2007, 10:18 PM

Yea what Nimlhûg said. Use css to create a text box, and cut off what's outside of it :)

skinner927

04-02-2007, 12:23 AM

Wow, I never thought of using a text box. the only issue I'd have with that is i'd like to do the '...' if I chop part of it off so it doesn't look much like an error. I am using CSS so the font size is set (although I know it varies from machine to machine to browser). So I was guessing I could do an "average" of what most browsers would see it as and do the chopping from that.

iLLin

04-02-2007, 12:28 AM

Think about what will happen if they turn the font size up in there browser. Something to keep in mind (I dont), but I/you should :p

skinner927

04-02-2007, 01:12 AM

If they turn the font size up, the heading won't be the only goofy thing :D

I'm making this for my school so I doubt any of the computers will have their font size turned up, so its not something to worry about. :p

iLLin

04-02-2007, 01:24 AM

Well you could use javascript to determine width of text based on browser and chop the text like that.