Cortar texto sem cortar a palavra…
Olá pessoal depois de muito tempo sem postar nada..
Vou mostrar pra vocês uma função bem simples e bem útil de cortar texto em cortar palvaras.
function crop_text($string,$lenght,$options){
$string = trim($string);
$end = array(' ', '.', ',', ';', ':', '!', '?');
if (strlen($string) <= $lenght)
return $string;
if (!in_array($string{$lenght - 1}, $end) && !in_array($string{$lenght}, $end))
while (--$lenght && !in_array($string{$lenght - 1}, $end));
return ( rtrim(substr($string,0,$lenght)).$options );
}
//Usando a função
echo crop_text($textoQueDesejaLimitar,$tamanhoLimite,'...');
Categories: php