paulomartinhago
8/4/2014 - 2:36 AM

Limita caracteres com reticências no final

Limita caracteres com reticências no final

<?php

require_once 'TextCharsLimit.php';

$text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellat cum, dolor eius vitae.';

echo TextCharsLimit::filterText($text, 20); // output: Lorem ipsum dolor...
<?php

class TextCharsLimit
{
	/**
	 * Trata o texto e retorna
	 * @param string $text
     * @param int    $limit Limite de caracteres
	 * @return string
	 */
	public static function filterText( $text, $limit )
	{
		if(!is_string($text) || !is_integer($limit))
			return $text;

		if(strlen($text) > $limit)
			$text = sprintf('%s...', mb_substr($text, 0, ($limit - 3), 'UTF-8'));

		return $text;
	}
}