Last active
January 4, 2016 22:44
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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