SiteKickr Web Development

Summarizing large amounts of text

There are a few tricks that can be used to summarize a body of text into a paragraph. One of the simplest methods is to simply grab the first n characters from the text and use it as the summary.

This method works well, but in its simplest form, shown below, you will likely end up with word fragments at the end of the summary.

Using ColdFusion as an example:

<cfoutput><p>#Left(text, 200)#</p></cfoutput>

Most languages either allow you to treat a string as a list with a specified delimitter, or to tokenize the string using a delimitter. In this case, the space (" ") will be our delimitter.

Again using ColdFusion as an example:

<cfset summary_length = 200>
<p>
  <cfif len(text) lt summary_length>
    #text#
  <cfelse>
    #ListDeleteAt(Left(text, summary_length), ListLen(Left(text, summary_length), " "), " ")#
    &hellip; <!--- a nice touch to let the viewer know there is more to the story --->
  </cfif>
</p>