Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RenzoTejada/c80e7ad2c9998d626ddc to your computer and use it in GitHub Desktop.
Save RenzoTejada/c80e7ad2c9998d626ddc to your computer and use it in GitHub Desktop.
Función que corta texto en una determinada cantidad de caracteres - wordpress - php
function cortarTexto($txt, $nr, $abrev = null) {
$tamano = $nr;
$contador = 0;
$texto = strip_tags($txt);
if ($texto != "") { // Cortamos la cadena por los espacios
//$arrayTexto = split(' ',$texto);
if ($tamano >= strlen($texto)) {
return $texto;
} else {
$arrayTexto = explode(' ', $texto);
$texto = '';
// Reconstruimos la cadena
while ($tamano >= strlen($texto) + strlen(@$arrayTexto[$contador])) {
$texto .= ' ' . @$arrayTexto[$contador];
$contador++;
}
return $texto . $abrev;
}
} else
return "Sin descripción";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment