Results 1 to 8 of 8

Thread: Reducing string length

  1. #1

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Reducing string length

    Hi there, now i have a string that i echo into html.

    I need to cut this string down if it exceeds the width of the table it resides in.

    I have looked into cutting it down according to how many characters it has, however as im using Tahoma, there is a big difference between 'w' and 'i'.

    Anyone have any ideas on how this can be done?

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Reducing string length

    Because of the way a browser can handle fonts (the USER has the ultimate decision on how big your font will display and can always override your font settings), I don't suggest that you try to do this on a pixel-by-pixel scale. It can be rather messy, especially when you're using a font that doesn't have same-sized letters like Courier or other native system/command-line-like fonts. The most I can tell you is to check how many W's fit in your table (the W is the widest letter, so it will be the best for you), and make the maximum number that, and cut off anything longer and then append some periods. This would work for that:
    PHP Code:
    <?php
      $max 
    6//maximum number of characters
      
    $str "blahblahblahblah";

      if(
    strlen($str) > $max)
        
    $dispstr substr($str0$max 1) . "..";
      else
        
    $dispstr $str;

      echo 
    $dispstr;
    ?>
    Sorry, though, that might not be completely what you were looking for, but it's about the only easy way.

    Alternatively, you could always go into an image editor and type out the entire alphabet in Tahoma, figure out each width in pixels and then store that value in an array, and then pick apart your string letter by letter to determine what the overall width is, and at what letter you can cut it off at. Like I said before, I don't suggest it, but you could always do it. It would be a lot slower, too.

    Edit: oh, and if you ever changed your font-face, you would have to figure out the new widths for each letter all over. that would be fun.
    Last edited by kows; Oct 11th, 2006 at 08:00 AM.
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: Reducing string length

    Thanks, well your seond option of measure the font, dare i say it seems really appealing, thanks for bringing the fact that users can infact change their font size.

    I will need to review this a little more thanks

  4. #4
    Addicted Member mfurqan's Avatar
    Join Date
    Oct 2005
    Location
    Pakistan
    Posts
    176

    Re: Reducing string length

    aren't there any space " " in string ?
    Muhammad Furqan Attari.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Reducing string length

    Code:
    td.limited { overflow: hidden; text-overflow-mode: ellipsis; }
    Note that that probably won't work in any current CSS implementation, so it's practically useless at this point in time.

  6. #6

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: Reducing string length

    That looks interesting, pen will that not work, with my current ttables?

    I will; take a look later, not at my dev machine now...

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Reducing string length

    No, it won't work on anything, and probably won't for a few years; but it's vaguely amusing to know about.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Reducing string length

    Quote Originally Posted by kows
    Edit: oh, and if you ever changed your font-face, you would have to figure out the new widths for each letter all over. that would be fun.
    And if the user doesn't have a Tahoma font (I don't on the Linux system), he'll still see it wrong.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width