Created
June 20, 2016 14:44
-
-
Save nathanrice/42222e5a02e80e1a4eac7e3b54e6a7a1 to your computer and use it in GitHub Desktop.
Account for a desired 0 characters in content limit
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
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