Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts

    elegant string cut

    I have a problem (obviously)

    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 I would appreciate it. Thanks!

  • #2
    New Coder
    Join Date
    Aug 2006
    Posts
    34
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yea what Nimlhûg said. Use css to create a text box, and cut off what's outside of it

  • #4
    New Coder
    Join Date
    Aug 2006
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #6
    New Coder
    Join Date
    Aug 2006
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    If they turn the font size up, the heading won't be the only goofy thing

    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.

  • #7
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well you could use javascript to determine width of text based on browser and chop the text like that.

    Code:
    function chopText(string) {
       if(length.is.to.long.chop.it.up) return choped.string;
    }
    
    //html
    <div class="page_title"><script type="...">chopText(<?php echo $string; ?>')...... blah blah u get the idea.
    Prolly more trouble than its worth but sounds like fun

  • #8
    New Coder
    Join Date
    Aug 2006
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Does anyone know of the code that will do this?

  • #9
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    JavaScript won't be able to figure out the string length in pixels.

    Have you considered using a fixed width font? Then you could set the font size, take that size and multiply by the header's character count.

    But this sounds all like a lot of work for nothing.

    The test name should just be limited to X characters in the set up. And if it does end up being longer, it should wrap to the next line. You shouldn't have to worry about this.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •