SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    North America
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question News Brief - Show first 'x' Number of Words

    I'm pulling data from MySQL and would like to show the first 'x' (number of words) so then I can place a link with [Read More..].

    Appreciate any help you may be able to give in this regard.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Location
    Amsterdam
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
        function getShortDescription($description='')
        {
            if(
    preg_match("/(.{50})(.*?)\W/"$description$m)) {
                
    $description $m[1] . $m[2] . "...";
            }

            if(
    strlen($description) > 70) {
                
    $description substr($description070) . "...";
            }

                    return 
    $description;
             } 
    This is an old snippet I've kept around.. It could be cleaned up and likely turned into a single regex, but it serves well for showing article summaries and such.

    The soft limit skips the first 50 characters, then seeks to the end of the remaining word to avoid cutting it off halfway through. Some jerk will inevitably notice this and try to break it by posting an enormous string of letters, hence the absolute 70 character cutoff.

  4. #4
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    North America
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you NikJazz. It does exactly what I need.

  5. #5
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    North America
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you rearden82.

    Appreciate it!


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
  •