-
-
Save parzibyte/eefd955060b3c53c66d4daa087e8b1ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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