Skip to content

Instantly share code, notes, and snippets.

@mohammadmursaleen
Created September 26, 2016 09:55
Show Gist options
  • Save mohammadmursaleen/eb2417ee921002735d0cf58d074d5bd5 to your computer and use it in GitHub Desktop.
Save mohammadmursaleen/eb2417ee921002735d0cf58d074d5bd5 to your computer and use it in GitHub Desktop.
Simple function to get truncated text to given count with last complete word
<?php
/**
* @author Mohammad Mursaleen
* @param $text
* @param $length
* @return mixed
*/
function mm_truncate( $text , $length ) {
$length = abs( (int)$length );
if( strlen($text) > $length ) {
$text = preg_replace("/^(.{1,$length})(\s.*|$)/s", '\\1', $text);
}
return($text);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment