Skip to content

Instantly share code, notes, and snippets.

@romelgomez
Created September 29, 2016 14:33
Show Gist options
  • Save romelgomez/5dc7a969c3231fce920d9b03fa453927 to your computer and use it in GitHub Desktop.
Save romelgomez/5dc7a969c3231fce920d9b03fa453927 to your computer and use it in GitHub Desktop.
Truncate text in PHP
<?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