Skip to content

Instantly share code, notes, and snippets.

@oreales
Forked from oscar-reales-interactiv4/truncate.php
Created February 12, 2014 23:09
Show Gist options
  • Save oreales/8966420 to your computer and use it in GitHub Desktop.
Save oreales/8966420 to your computer and use it in GitHub Desktop.
<?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