Created
June 22, 2011 20:52
-
-
Save chucktrukk/1041151 to your computer and use it in GitHub Desktop.
This file contains 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 | |
function trim_text($input, $length, $ellipses = true, $strip_html = true, $output_entities = true){ | |
// Strip tags, if desired | |
if($strip_html) { | |
$input = strip_tags($input); | |
} | |
// No need to trim, already shorter than trim length | |
if(strlen($input) <= $length) { | |
return $input; | |
} | |
// Find last space within length | |
$last_space = strrpos(substr($input, 0, $length), ' '); | |
$trimmed_text = substr($input, 0, $last_space); | |
// Add ellipses (...) | |
if($ellipses){ | |
$trimmed_text .= '...'; | |
} | |
if($output_entities){ | |
$trimmed_text = htmlentities($trimmed_text); | |
} | |
return $trimmed_text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment