Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 24

Thread: Shorten a string?

  1. #11
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Works, but one long word (or just a random string of characters, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...) would bypass it.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  2. #12
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    //shrug

    PHP Code:
    function cut_it($str,$len=20) { //default is 20, but you can switch to anything you want here on when calling it
    if (strlen($str)<=$len) { return $str; }
    $str wordwrap($str,$len); //defaults to the behaviour twey setup
    $str explode("\n",$str,2); //lazy-- you could use substr/strpos, etc
    $str $str[0];
    if (
    $str>$len) {
    $str substr($str,0,$len-3).'...';
    }
    return 
    $str;
    }
    $test 'whatever and stuff this is some text etc';
    $test cut_it($test,15); 
    Anyway, your function probably makes more sense. If anyone wants to use this, you could (or I would) make the ending '...' dynamic.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #13
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Mm. Handy though. Here:
    Code:
    function truncate_string($tr, $len, $tail = '...') {
      if(strlen($tr) > $len) {
        $tr = explode("\n", wordwrap(str_replace("\n", "\0", $tr), $len), 2);
        $tr = str_replace("\0", "\n", substr($tr[0], 0, $len) . $tail);
      }
      return $tr;
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #14
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Code:
    function truncate_string($tr, $len, $tail = '...') {
      if(strlen($tr) > $len) {
    $len = $len-3;
        $tr = explode("\n", wordwrap(str_replace("\n", "\0", $tr), $len), 2);
        $tr = str_replace("\0", "\n", substr($tr[0], 0, $len) . $tail);
      }
      return $tr;
    }
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #15
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No. For a start I just don't think that's a good idea: the actual string will be truncated shorter than the specified number of characters. If I say I want 20 characters of the string to be displayed, I'd be mightily annoyed if it actually got cut down to 17, and that would take me a while to debug if this function was closed off in a black box somewhere. It's just not intuitive.

    Secondly, if it were to be done, you'd need strlen($tail) not a static 3, you'd have to take into account what to do if the tail were longer than $len, &c., none of which options are at all intuitive in my opinion.

    Thirdly, what's wrong with $len -= 3;?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #16
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    -=3 is fine.

    Well, I can see what you mean about it being ambiguous, but having the string longer than the allowed maximum is strange. You have only 20 characters of space, but then need room for the elipses?

    And, yes, you're right-- strlen($tail).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #17
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You have only 20 characters of space, but then need room for the elipses?
    I might set it to 20 then be a little puzzled when it didn't quite fit, but I'd work it out pretty quickly. If I set it to 20 and only got 17 characters, however, I wouldn't immediately recognise the source of the bug.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #18
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    If I set it to 20 and only got 17 characters
    No... it WOULD Be 20 characters. Do dots not count? You are outputting a string to fit in a small space. (Let's assume this is courier, or another such font, so we don't need to consider [as it would be very difficult anyway, though perhaps a fun task for GD] the width of individual characters].)

    You have 20 spots-- you get 20 characters. Good. You get 23... and... problem.

    Though it's only 17 of CONTENT, that still is how much space you have. To maximize content, use no tail.


    As for having a tail longer than the character limit... uh... that defeats the point. It's like asking "How many pickup trucks can I fix under my table?"-- and hopefully no one would do that.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #19
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No... it WOULD Be 20 characters. Do dots not count?
    Twenty characters of the string.
    As for having a tail longer than the character limit... uh... that defeats the point. It's like asking "How many pickup trucks can I fix under my table?"-- and hopefully no one would do that.
    That's true, but in a real case you'd have to deal with it somehow.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #20
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Twenty characters of the string.
    No. The idea isn't twenty characters of the string, or twenty you like, or twenty you want-- it's twenty that fits within a certain area. If you JUST want the string, go ahead and remove the ellipses... guess that serves no point. So, use no tail, and then you get up to exactly 20 characters.

    That's true, but in a real case you'd have to deal with it somehow.
    In a real case where someone wants to place pickup trucks under a table? There might be someone (probably drunk) who would try such a thing. But it doesn't mean anyone really wants to. If you really want to add a longer string to the end, it's a different situation entirely.

    You could set the function to return an array, the second parameter of which is if the string needs the tail or not (which, if it's longer, would probably actually not need it), then check that and add it manually after in such a situation.

    I can't think of a situation where I'd want to have it return the number + tail*, nor one in which I'd want a bizarre tail.

    (*Actually, just setting the initial functionc all for thisfunc($str,$len+strlen($tail),$tail) would give a reasonable response because you'd get up to ±3 characters, then, once it was ±3 less than the max length, it would add on the tail. Guess it would work.)


    Anyway, there are theoretical needs for making this function handle anything, but there's a reason that the space shuttle doesn't have a propeller and torpedoes.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

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