Do you wanna show limited text of long text content? if you are using php, then use this simple handy function, else use this logic to build your own function in your programming language.
Here is the procedure we used to create our handy function.
Function definition:
Here is the procedure we used to create our handy function.
- First we will apply strip_tags() function on our string to avoid breaking any html.
- And then compare our string length with limit parameter. If string length is lesser than limit parameter value, then return the same string. If it is greater than limit parameter value, then turncate the string using substr() function with our limit parameter.
- To avoid breaking word, check the string ending with any words, If it is a separated with space, then break it up.
Function definition:
function limitText($string, $limit=200, $concatStr){ //strip tags to avoid breaking any html $string = strip_tags($string); if(strlen($string) > $limit){ //turncate string $stringCut = substr($string,0,$limit+1); //make sure it ends in a word. //if the ending word is a long text and it exceeds the limit, then break it up $string = (strrpos($stringCut, ' '))?substr($stringCut,0,strrpos($stringCut, ' ')).$concatStr:$stringCut.$concatStr; } return $string; }Function usage:
$limitedText = limitText($string, 100, '...');
Have any doubt, feel free to comment here!