Created
March 19, 2015 02:35
-
-
Save iTonyYo/f9da5eeccefa98e356ee to your computer and use it in GitHub Desktop.
Trim a block of text to a specified amount of characters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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