Skip to content

Instantly share code, notes, and snippets.

@djacobs
Created July 7, 2010 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djacobs/467413 to your computer and use it in GitHub Desktop.
Save djacobs/467413 to your computer and use it in GitHub Desktop.
function createExcerpt(str,maxwords){
if(str) {
var excerpt = '';
var words_arr = [];
str = str.replace(/^\s+|\s+$/g,""); //trims spaces from start and end of string
str = stripTags(str);
words_arr=str.split(' ');
if(words_arr.length > maxwords) {
for (i=0; i < maxwords; i++) {
excerpt += words_arr[i] + ' ';
}
return excerpt + '...';
} else {
return str;
}
} else {
return '...';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment