Si se quiere convertir, por ejemplo á por á y todas las demás, con esta simple función ya vale. Su funcionamiento:
1. Convierte TODOS los caracteres especiales a entidades.
2. Como los caracteres <, & y > no nos interesa que queden codificados, se vuelven a descodificar (sólo estos).
function caracteres_html($texto){ $texto = htmlentities($texto, ENT_NOQUOTES, 'UTF-8'); // Convertir caracteres especiales a entidades $texto = htmlspecialchars_decode($texto, ENT_NOQUOTES); // Dejar <, & y > como estaban return $texto; }
Si no se dispone de PHP 5 o posterior, se necesita también agregar el siguente código:
if ( !function_exists('htmlspecialchars_decode') ) { function htmlspecialchars_decode($text) { return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS))); } }
A disfrutarlo!