Skip to content

Instantly share code, notes, and snippets.

@oscar-reales-interactiv4
Created March 15, 2012 18:59
Show Gist options
  • Save oscar-reales-interactiv4/2046059 to your computer and use it in GitHub Desktop.
Save oscar-reales-interactiv4/2046059 to your computer and use it in GitHub Desktop.
Truncate long text function
<?php
/**
* Esta funcion devuelve una cadena pasada como parametro
* truncada a un maximo de caracteres segun parametro
* @param string $string
* @param int $maxLength
* @return string
*/
public function truncateString($string, $maxLength)
{
if(mb_strlen($string) > $maxLength)
{
$maxLength = $maxLength - 3; //para los puntos suspensivos
return mb_substr($string, 0, $maxLength) . "...";
} else {
return $string;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment