diff options
author | Greg Aker <greg.aker@ellislab.com> | 2010-08-05 21:09:20 +0200 |
---|---|---|
committer | Greg Aker <greg.aker@ellislab.com> | 2010-08-05 21:09:20 +0200 |
commit | cbe3247819be75c34231ea200874044735bd853b (patch) | |
tree | 1545cc5f40bcf55b40fc14bd27179b0bf346fbf7 /system/helpers | |
parent | 2ddc9496e9403a59a87b644d1c2b9a106b773e46 (diff) |
Adding an ellipsize function to the text helper, and associated documentation to the user guide.
Diffstat (limited to 'system/helpers')
-rw-r--r-- | system/helpers/text_helper.php | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 477260216..b7ade7a8f 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -495,6 +495,48 @@ if ( ! function_exists('word_wrap')) } } +// ------------------------------------------------------------------------ + +if ( ! function_exists('ellipsize')) +{ + /** + * Ellipsize String + * + * This function will strip tags from a string, split it at its max_length and ellipsize + * + * @param string string to ellipsize + * @param integer max length of string + * @param mixed int (1|0) or float, .5, .2, etc for position to split + * @param string ellipsis ; Default '...' + * @return string ellipsized string + */ + function ellipsize($str, $max_length, $position = 1, $ellipsis = '…') + { + // Strip tags + $str = trim(strip_tags($str)); + + // Is the string long enough to ellipsize? + if (strlen($str) <= $max_length) + { + return $str; + } + + $beg = substr($str, 0, floor($max_length * $position)); + + $position = ($position > 1) ? 1 : $position; + + if ($position === 1) + { + $end = substr($str, 0, -($max_length - strlen($beg))); + } + else + { + $end = substr($str, -($max_length - strlen($beg))); + } + + return $beg.$ellipsis.$end; + } +} /* End of file text_helper.php */ /* Location: ./system/helpers/text_helper.php */
\ No newline at end of file |