Skip to content

Instantly share code, notes, and snippets.

@nathanrice
Created June 20, 2016 14:44
Show Gist options
  • Save nathanrice/42222e5a02e80e1a4eac7e3b54e6a7a1 to your computer and use it in GitHub Desktop.
Save nathanrice/42222e5a02e80e1a4eac7e3b54e6a7a1 to your computer and use it in GitHub Desktop.
Account for a desired 0 characters in content limit
function genesis_truncate_phrase( $text, $max_characters ) {
if ( ! $max_characters ) {
return '';
}
$text = trim( $text );
if ( mb_strlen( $text ) > $max_characters ) {
//* Truncate $text to $max_characters + 1
$text = mb_substr( $text, 0, $max_characters + 1 );
//* Truncate to the last space in the truncated string
$text_trim = trim( mb_substr( $text, 0, mb_strrpos( $text, ' ' ) ) );
$text = empty( $text_trim ) ? $text : $text_trim;
}
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment