Created
September 26, 2016 09:55
-
-
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
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
<?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