Last active
August 29, 2015 13:56
-
-
Save reesmcivor/8844959 to your computer and use it in GitHub Desktop.
Truncate Words
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 | |
/** | |
* Truncate string | |
* -------------------------------------------------------------------------------------------- | |
* @param str string subject to truncate | |
* @param len integer length of the string after being stripped from tags if true | |
* @param ending string concatinate $ending if the string has been truncated | |
* @param bStripTags bool strip the html tags from string | |
*/ | |
function truncate($str, $len = 120, $ending = '...', $bStipTags = true, $bnl2br = true) { | |
if(!$str) { | |
return false; | |
} | |
// Strip the tags from the $str | |
if($bStipTags) { | |
$str = strip_tags($str); | |
} | |
if(strlen($str) > $len) { | |
$str = substr($str, 0, $len) . '...'; | |
} | |
// Return with \n to <br /> if set to true | |
return $bnl2br ? nl2br($str) : $str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment