bishawjit-das
1/24/2017 - 5:51 PM

truncate a string only at a whitespace

truncate a string only at a whitespace

//truncate a string only at a whitespace
function truncate($text, $length) {
	$length = abs((int)$length);
	if(strlen($text) > $length) {
		$text = preg_replace("/^(.{1,$length})(\s.*|$)/s", '\\1', $text);
	}
	return($text);
}