← All posts

[ITA] Rimuovere le entità HTML (unhtmlentities)

Ecco un modo veloce per rimuovere tutte le entità html da una stringa:

function unhtmlentities($string) {  
  $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);  
  $string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string);  
  $trans_tbl = get_html_translation_table(HTML_ENTITIES);  
  $trans_tbl = array_flip($trans_tbl);  
  return strtr($string, $trans_tbl);  
}

questa funzione sostituisce sia le entità numeriche che quelle testuali.