Skip to content

Instantly share code, notes, and snippets.

@reesmcivor
Last active August 29, 2015 13:56
Show Gist options
  • Save reesmcivor/8844959 to your computer and use it in GitHub Desktop.
Save reesmcivor/8844959 to your computer and use it in GitHub Desktop.
Truncate Words
<?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