Return to Snippet

Revision: 5307
at February 27, 2008 03:58 by OscarWilde


Initial Code
function entiConv($txt) {
  // Liste mit Umlauten abfragen und Klammern rauslöschen:
  $table = get_html_translation_table(HTML_ENTITIES);
  unset($table['<']);
  unset($table['>']);

  // Das Zeichen & ganz oben im Array platzieren, damit dieses Zeichen in den Ersetzungen nicht nochmal ersetzt wird:
  $temp = $table['&'];
  unset($table['&']);
  $table = array_reverse($table, true);
  $table['&'] = $temp;
  $table = array_reverse($table, true);

  // Erst die Entities in echte Werte umwandeln - Beispiel String:
  // "Hallo Entities &amp; Leser und & Programmierer" wird zu "Hallo Entities & Leser und & Programmierer"
  foreach($table as $key => $value) {
     if($key == "\"") {
       // Anführungsstriche nur außerhalb von HTML-Tags ersetzen:
       $txt = preg_replace("/((<[^>]*)|$value)/e", '"\2"=="\1" ? "\1" : "$key"', $txt);
     } else {
       $txt = preg_replace("/$value/", $key, $txt);            
     }
  }
  // Dann die echten Werte in Entities umwandeln - Beispiel String:
  // "Hallo Entities & Leser und & Programmierer" wird zu "Hallo Entities &amp; Leser und &amp; Programmierer"
  foreach($table as $key => $value) {
    if($key == "\"") {
      // Anführungsstriche nur außerhalb von HTML-Tags ersetzen:
      $txt = preg_replace("/((<[^>]*)|$key)/e", '"\2"=="\1" ? "\1" : "$value"', $txt);
    } else {
      $txt = preg_replace("/$key/", $value, $txt);            
    }
  }
  return $txt;
}

Initial URL


Initial Description
Diese Funktion wendelt Entities in HTML Quellcode um, ohne dabei den HTML Quellcode zu zerstören.

Initial Title
Convert HTML-Entities and prevent HTML-Tags from changing

Initial Tags
convert

Initial Language
PHP