Created
September 29, 2016 14:33
-
-
Save romelgomez/5dc7a969c3231fce920d9b03fa453927 to your computer and use it in GitHub Desktop.
Truncate text in PHP
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 | |
function truncate($text, $chars = 25) { | |
$text = $text." "; | |
$text = substr($text,0,$chars); | |
$text = substr($text,0,strrpos($text,' ')); | |
$text = $text."..."; // Si no se desea tener tres puntos suspensivos se comenta esta línea. | |
return $text; | |
} | |
// El nombre es truncado a 20 caracteres, luego coloca ... puntos suspensivos. | |
$text = 'Romel Javier Gomez Herrera'; | |
echo truncate($text, 20); | |
// Fuente: http://stackoverflow.com/questions/9219795/truncating-text-in-php |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment