Skip to content

Instantly share code, notes, and snippets.

@PetrovStark
Last active October 31, 2019 21:47
Show Gist options
  • Save PetrovStark/245f2225cb763158923f53d70979b238 to your computer and use it in GitHub Desktop.
Save PetrovStark/245f2225cb763158923f53d70979b238 to your computer and use it in GitHub Desktop.
// ============
// Excerpt Limiter - Desenvolvido por SamuraiPetrus (https://github.com/SamuraiPetrus)
// (Limitador de string que corta o texto no último espaço em branco)
//=============
// Parâmetros
//=============
//$text -> (str) Texto a ser limitado.
//$chars -> (int) (default: 100) Número de caracteres do texto final.
function excerpt_limiter($str, $chars=100){
if (strlen($str) >= $chars){
$str = substr($str, 0, $chars);
$split = str_split($str);
$final = $chars - 1;
if ($split[$final] != " "){
$aux = $final;
while ($split[$aux] != " ") {
unset($split[$aux]);
$aux -= 1;
}
$real_chars = sizeof($split);
return substr($str, 0, $real_chars) . "[...]";
}else{
return substr($str, 0, $chars). "[...]";
}
}else if (strlen($str) < $chars){
return $str;
}else{
return "erro desconhecido, favor contatar o desenvolvedor da função.";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment