Flowdock
excerpt(text, phrase, radius = 100, excerpt_string = "...") public

Extracts an excerpt from text that matches the first instance of phrase. The radius expands the excerpt on each side of the first occurrence of phrase by the number of characters defined in radius (which defaults to 100). If the excerpt radius overflows the beginning or end of the text, then the excerpt_string will be prepended/appended accordingly. If the phrase isn’t found, nil is returned.

Examples

  excerpt('This is an example', 'an', 5)
  # => "...s is an examp..."

  excerpt('This is an example', 'is', 5)
  # => "This is an..."

  excerpt('This is an example', 'is')
  # => "This is an example"

  excerpt('This next thing is an example', 'ex', 2)
  # => "...next t..."

  excerpt('This is also an example', 'an', 8, '<chop> ')
  # => "<chop> is also an example"
Show source
Register or log in to add new notes.
September 23, 2008 - (<= v2.1.0)
1 thank

Description copied from Rails 2.0

Extracts an excerpt from text that matches the first instance of phrase. The radius expands the excerpt on each side of the first occurrence of phrase by the number of characters defined in radius (which defaults to 100). If the excerpt radius overflows the beginning or end of the text, then the excerpt_string will be prepended/appended accordingly. If the phrase isn‘t found, nil is returned.

Examples

excerpt('This is an example', 'an', 5)
# => "...s is an examp..."

excerpt('This is an example', 'is', 5)
# => "This is an..."

excerpt('This is an example', 'is')
# => "This is an example"

excerpt('This next thing is an example', 'ex', 2)
# => "...next t..."

excerpt('This is also an example', 'an', 8, '<chop> ')
# => "<chop> is also an example"