Mi sono trovato a richiedere questa funzione e mi chiedevo se esiste già in PHP.Questa funzione esiste in PHP?
/**
* Truncates $str and returns it with $ending on the end, if $str is longer
* than $limit characters
*
* @param string $str
* @param int $length
* @param string $ending
* @return string
*/
function truncate_string($str, $length, $ending = "...")
{
if (strlen($str) <= $length)
{
return $str;
}
return substr($str, 0, $length - strlen($ending)).$ending;
}
Quindi, se il limite è di 40 e la stringa è "La volpe veloce saltato il pigro cane marrone", l'uscita sarebbe "La volpe veloce saltato oltre la cima pigro ...". Sembra il genere di cosa che esisterebbe in PHP, quindi sono rimasto sorpreso quando non sono riuscito a trovarlo.
questo non va bene, perché aggiunge il suffisso di una stringa più breve di $ maxLength. –
È vero, dovresti comunque includerlo nella tua funzione, che esegue solo substr_replace() in base a tale condizione. –