Snipe.Net - Geeky Stuff
Twitter
Currently: @DroolingHamster heh. Naw, just work. So, actually.... in reply to DroolingHamster 7 hrs ago

Truncate text to x number of words

closeThis post was published 9 years 7 months 6 days ago. There is a chance that some APIs or software versions discussed have changed since this article was written.

This code will truncate given text to a certain length, adding “…” where it’s been truncated. For example:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Quisque ac urna a erat pharetra placerat. Sed at tortor id lacus pellentesque malesuada. Suspendisse vitae dui vel velit tincidunt aliquet. Cras condimentum lobortis leo. Integer semper. Nunc semper felis a neque malesuada posuere. Quisque convallis dolor sed erat. Mauris tellus. Nulla vestibulum diam sed felis. Aenean mi orci, fringilla at, vulputate dapibus, cursus ut, sem.

Would be turned into:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Quisque ac…

Shorten text
 $shortened_text_num ) {
        $shortened_text .= "... ";
    }
    return strip_tags($shortened_text);

}
?>

Also check out:

If you think this article kicked ass, subscribe to the RSS feed or follow me on Twitter! Share with your friends, or leave a comment below (or better still, do both!) My entire concept of self-worth is in your hands, so that makes you kind of a big deal. Srsly.

This entry was posted on Thursday, June 27th, 2002 at 11:52 am and is filed under PHP/mySQL. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.