Advanced Search

Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Shorten a string?

  1. #1
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default Shorten a string?

    Hey, there's a string I'm taking from a database and displaying. I would like to shorten that string if it is longer than a certain number of characters and add "..." at the end. How would I do this? Thanks.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Check out the following thread:
    http://www.dynamicdrive.com/forums/s...ghlight=substr

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

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

    Default

    Better would be a soft limit in words, then a hard limit in characters:
    Code:
    function truncate_passage($tr, $wordlimit = 20, $charlimit = 0, $tail = '...') {
      $utail = false;
    
      if($wordlimit) {
        $tr = explode(' ', $tr);
        $utail = count($tr) > $wordlimit;
        $tr = implode(' ', array_slice($tr, 0, $wordlimit));
      }
    
      if($charlimit) {
        $utail = $utail || strlen($tr) > $charlimit;
        $tr = substr($tr, 0, $charlimit);
      }
    
      if($utail) $tr .= $tail;
    
      return $tr;
    }
    Last edited by Twey; 12-04-2007 at 03:45 PM. Reason: Typo fix.
    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. #4
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    I think I may have a use for both. Thanks guys
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  5. #5
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    only question I have is what is "utail" ?

    Edit: BEGIN

    Quote Originally Posted by alexjewell
    I think I may have a use for both. Thanks guys
    that script allows both. truncate_passage($tr, $wordlimit = 20, $charlimit = 0, $tail = '...')

    Edit: END
    Last edited by boogyman; 12-04-2007 at 01:06 PM. Reason: added response

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

    Default

    alexjewell, there's nothing that does that mine doesn't.

    boogyman, whether to append the tail or not. I called it $tail, then made it into a function, and $tail was the logical name for the fourth parameter, so I had to rename 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!

  7. #7
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    I used substr and strlen:

    Code:
    if(strlen($row['Title'])){ $row['Title'] = substr($row['Title'],0,25).'...';}
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  8. #8
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by alexjewell View Post
    I used substr and strlen:

    Code:
    if(strlen($row['Title'])){ $row['Title'] = substr($row['Title'],0,25).'...';}
    that will be fine if this is the one and only time you will EVER need to do this, including not only in this script but forever, which the liklihood of you encountering something else you would like to limit is very very slim.

    Twey was giving you a generic fuction that you could re-use over and over and over. and cover a number of parameters and options.

    but we aren't here to dictate how you write your site, so I'm happy you found the solution. a good reference is www.php.net which is the homesite of the programming language, and it is very well documented.


    Twey, I thought it was just a check to see whether or not to include the tail but what shook me was
    $utail = count($tr) > $wordlimit;
    $utail = $utail || strlen($tr) > $charlimit;
    That first one is the one that threw me off because its setting the tail by default where the second is creating a check first?

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

    Default

    I used substr and strlen:
    The main problem with this is that it's liable to cut off the string halfway through a word: "I welcome you all here tod" while mine will try not to do so where possible: "I welcome you all here today..." That was the main problem I was trying to solve. The hard character limit is off by default but should probably be set too, since otherwise one very long word wouldn't get truncated.
    That first one is the one that threw me off because its setting the tail by default where the second is creating a check first?
    What? The former is equivalent to:
    Code:
    if(count($tr) > $wordlimit) // if there are more words than allowed
      $utail = true;
    And the latter:
    Code:
    if($utail || strlen($tr) > $charlimit)
      $utail = true;
    Without the check for $utail, the function wouldn't append the dots if the string were truncated by words and then was shorter than the character limit.
    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. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    260
    Thanked 690 Times in 678 Posts

    Default

    Another thought--
    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
    return $str[0];
    }
    $test 'whatever and stuff this is some text etc';
    $test cut_it($test,15); 
    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
  •