Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Character Limiter
- *
- * Limits the string based on the character count. Preserves complete words
- * so the character count may not be exactly as specified.
- *
- * @access public
- * @param string
- * @param integer
- * @param string the end character. Usually an ellipsis
- * @return string
- */
- if ( ! function_exists('character_limiter'))
- {
- function character_limiter($str, $n = 500, $end_char = '…')
- {
- if (strlen($str) < $n)
- {
- return $str;
- }
- $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
- if (strlen($str) <= $n)
- {
- return $str;
- }
- $out = "";
- foreach (explode(' ', trim($str)) as $val)
- {
- $out .= $val.' ';
- if (strlen($out) >= $n)
- {
- $out = trim($out);
- return (strlen($out) == strlen($str)) ? $out : $out.$end_char;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement