Skip to content

Instantly share code, notes, and snippets.

@iTonyYo
Created March 19, 2015 02:35
Show Gist options
  • Save iTonyYo/f9da5eeccefa98e356ee to your computer and use it in GitHub Desktop.
Save iTonyYo/f9da5eeccefa98e356ee to your computer and use it in GitHub Desktop.
Trim a block of text to a specified amount of characters.
/**
* Trim a block of text to a specified amount of characters.
*
* @param string elem string to 'cut'.
* @param integer length Character count/length. Default 30.
* @param string more Text to display as 'more'. Default '...'
*
* @return string
*/
$.textExcerpt = function (elem, length, more) {
if ('undefined' == typeof elem || '' == elem) {
return false;
}
length = length || 30,
more = more || '...';
var cut = elem.indexOf(' ', length);
if (-1 == cut) {
return elem + more;
}
return elem.substring(0, cut) + more;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment