Limpia texto de etiquetas html
Quita etiquetas, espacios, caracteres no deseados
Genera texto sin etiquetas html, php, espacios, etc. Texto limpio por ejemplo para generar la descripción o el título en las meta etiquetas para optimizar el SEO.
/* TEST DE LA FUNCIÓN */
<meta description="<?=text_description('texto con espacios, <div>etiquetas</div> o cualquier codigo html generado')?>">
/* FUNCIÓN PARA LIMPIAR TEXTOS */
function text_description($text, $limit = 160)
{
$count = strlen($text);
$srch = array('/\>[^\S ]+/s','/[^\S ]+\</s','/(\s)+/s');
$rmpl = array('>','<','\\1');
$text = preg_replace($srch, $rmpl, $text);
$text = str_replace("> <", "><", $text);
$text = preg_replace("#\s{2,}#"," ",$text);
$text = strip_tags(trim(html_entity_decode($text, ENT_QUOTES, 'UTF-8'), "\xc2\xa0"));
$text = preg_replace("/[\n\r]/","",$text);
$text = stripslashes($text);
$text = strip_tags($text);
#$text = mysql_real_escape_string($text);
$text = trim($text);
$text = substr($text, 0,$limit);
if($count > $limit){
$index = strrpos($text, " ");
$text = substr($text, 0,$index);
$text .= "...";
}
return $text;
}