Results 1 to 3 of 3

Thread: Cut off a given string automatically?

  1. #1
    Join Date
    Apr 2006
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cut off a given string automatically?

    OK, if you view this RSS feed in Firefox 2+, you would've noticed that descriptions get cut off automatically. Unfortunately, when something like an RSS parser in PHP reads the RSS feed, the description is the entire blog post.

    So is it possible you could have something like this where feeding a function or something a variable containing a long string, and just cutting it off after a specified number of words, then adding [...]. Thanks!
    Last edited by rctxtreme; 07-16-2007 at 02:33 AM.

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

    Default

    Code:
    function abbreviate($paragraph, $length = 30, $final = '...') {
      $words = explode(' ', $paragraph);
      if(count($words) <= $length)
        return $paragraph;
      else
        return implode(' ', array_slice($words, 0, $length + 1)) . $final;
    }
    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!

  3. #3
    Join Date
    Apr 2006
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Now this is much better than the first thing I found. Thanks a lot =D

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
  •