Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created April 3, 2019 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/eefd955060b3c53c66d4daa087e8b1ee to your computer and use it in GitHub Desktop.
Save parzibyte/eefd955060b3c53c66d4daa087e8b1ee to your computer and use it in GitHub Desktop.
<?php
/*
Tomada de: https://github.com/laravel/framework/blob/5.8/src/Illuminate/Support/Str.php#L212
*/
function str_limit($value, $limit = 100, $end = '...'){
if (mb_strwidth($value, 'UTF-8') <= $limit) {
return $value;
}
return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
}
// Formas de uso
# Limitar a 3 caracteres y si es más larga cortarla, agregándole puntos suspensivos
echo str_limit("Hola mundo soy una cadena muy larga", 3, "...");
echo "\n\n";
# Limitar a 15 caracteres y si es más larga cortarla, pero no agregar nada al final
echo str_limit("Hola mundo soy una cadena muy larga", 15);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment