Skip to content

Instantly share code, notes, and snippets.

@chucktrukk
Created June 22, 2011 20:52
Show Gist options
  • Save chucktrukk/1041151 to your computer and use it in GitHub Desktop.
Save chucktrukk/1041151 to your computer and use it in GitHub Desktop.
<?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