Add a (Continue...) link instead of displaying full text

Hello,

I have quite a number of long texts which I don’t want to display in their full extent.

The best approach would be to display say 300 first chars, then find nearest whiste space (after the 300th character) and append the “Continue” link.

I must admit that I have no idea how to do it. Should I look into string functions? RegExp?

Thanks in advance :slight_smile:

You can use this to truncate your text and then insert the link.

#====================================================================================
# truncates text based upon the number of characters and pads with a space (default)
#====================================================================================
function truncate_chars($string, $limit, $break=' ', $pad='...') {
	// return with no change if string is shorter than $limit
	if(strlen($string) <= $limit) return $string;
	
	$string = substr($string, 0, $limit);
	if(false !== ($breakpoint = strrpos($string, $break))) {
		$string = substr($string, 0, $breakpoint);
	}
	return $string . $pad;
}

echo truncate_chars($string,300) . ' <a href="link_to_post/">continue</a>';