Searching for a fast replacement of the MS WORD special characters which are not covered by get_html_translation_table() , I think the following function might help someone
<?php
function clean_up($str){
$str = stripslashes($str);
$str = strtr($str, get_html_translation_table(HTML_ENTITIES));
$str = str_replace( array("\x82", "\x84", "\x85", "\x91", "\x92", "\x93", "\x94", "\x95", "\x96", "\x97"), array("‚", "„", "…", "‘", "’", "“", "”", "•", "–", "—"),$str);
return $str;
}
?>
It replaces all types of quotes (single and double), horizontal ellipsis (...), bullet, en dash and em dash.
get_html_translation_table
(PHP 4, PHP 5)
get_html_translation_table — Gibt die Umwandlungs-Tabelle zurück, die von htmlspecialchars() und htmlentities() verwendet wird
Beschreibung
$table = HTML_SPECIALCHARS
[, int $flags = ENT_COMPAT | ENT_HTML401
[, string $encoding = 'UTF-8'
]]] )get_html_translation_table() gibt die Umwandlungs-Tabelle zurück, die intern in den Funktionen htmlspecialchars() und htmlentities() verwendet wird.
Hinweis:
Sonderzeichen können auf unterschiedliche Weise kodiert werden. " kann kodiert werden als ", " oder ". get_html_translation_table() gibt nur die von htmlspecialchars() und htmlentities() genutze Form zurück.
Parameter-Liste
-
table -
Welche Tabelle zurückgegeben werden soll. Entweder
HTML_ENTITIESoderHTML_SPECIALCHARS. -
flags -
Eine Bitmaske von einem oder mehreren der folgenden Flags, welche festlegen welche Anführungszeichen die Tabelle enthalten wird, sowie für welchen Dokumenttyp sie ist. Der Standardwert ist ENT_COMPAT | ENT_HTML401.
Verfügbare flags-KonstantenKonstantenname Beschreibung ENT_COMPATTabelle soll Entities für doppelte Anführungszeichen enthalten, aber nicht für einfache. ENT_QUOTESTabelle soll Entities sowohl für einfache, als auch doppelte Anführungszeichen enthalten. ENT_NOQUOTESTabelle soll weder Entities für einfache, noch für doppelte Anführungszeichen enthalten. ENT_HTML401Tabelle für HTML 4.01. ENT_XML1Tabelle für XML 1. ENT_XHTMLTabelle für XHTML. ENT_HTML5Tabelle für HTML 5. -
encoding -
Definiert die zu verwendende Zeichenkodierung. Standardwert ist ISO-8859-1 in PHP Versionen vor 5.4.0 und UTF-8 in PHP 5.4.0 und neuer.
Die folgenden Zeichensätze werden mit PHP 4.3.0 und höher unterstützt:
Unterstützte Zeichensätze Zeichensatz Alias Beschreibung ISO-8859-1 ISO8859-1 Westeuropäisch, Latin-1 ISO-8859-15 ISO8859-15 Westeuropäisch, Latin-9. Enthält das Euro-Zeichen sowie französische und finnische Buchstaben, die in Latin-1(ISO-8859-1) fehlen. UTF-8 ASCII-kompatibles Multi-Byte 8-Bit Unicode. cp866 ibm866, 866 DOS-spezifischer Kyrillischer Zeichensatz. Dieser Zeichensatz wird ab PHP Version 4.3.2 unterstützt. cp1251 Windows-1251, win-1251, 1251 Windows-spezifischer Kyrillischer Zeichensatz. Dieser Zeichensatz wird ab PHP Version 4.3.2 unterstützt. cp1252 Windows-1252, 1252 Windows spezifischer Zeichensatz für westeuropäische Sprachen. KOI8-R koi8-ru, koi8r Russisch. Dieser Zeichensatz wird ab PHP Version 4.3.2 unterstützt. BIG5 950 Traditionelles Chinesisch, hauptsächlich in Taiwan verwendet. GB2312 936 Vereinfachtes Chinesisch, nationaler Standard-Zeichensatz. BIG5-HKSCS Big5 mit Hongkong-spezifischen Erweiterungen; traditionelles Chinesisch. Shift_JIS SJIS, 932 Japanisch EUC-JP EUCJP Japanisch Hinweis: Weitere Zeichensätze sind nicht implementiert, an ihrer Stelle wird ISO-8859-1 verwendet.
Rückgabewerte
Gibt die Umwandlungstabelle als Array zurück, wobei die ursprünglichen Zeichen die Schlüssel sind und die Entities die Werte.
Changelog
| Version | Beschreibung |
|---|---|
| 5.4.0 |
Der Standardwert für encoding wurde
zu UTF-8 geändert.
|
| 5.4.0 |
Die Konstanten ENT_HTML401,
ENT_XML1, ENT_XHTML
und ENT_HTML5 wurden hinzugefügt.
|
| 5.3.4 |
Der encoding parameter wurde
hinzugefügt.
|
Beispiele
Beispiel #1 Umwandlungs-Tabellen-Beispiel
<?php
var_dump(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES | ENT_HTML5));
?>
Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:
array(1510) {
[" "]=>
string(5) "	"
["
"]=>
string(9) "
"
["!"]=>
string(6) "!"
["""]=>
string(6) """
["#"]=>
string(5) "#"
["$"]=>
string(8) "$"
["%"]=>
string(8) "%"
["&"]=>
string(5) "&"
["'"]=>
string(6) "'"
// ...
}
Siehe auch
- htmlspecialchars() - Wandelt Sonderzeichen in HTML-Codes um
- htmlentities() - Wandelt alle geeigneten Zeichen in entsprechende HTML-Codes um
- html_entity_decode() - Konvertiert alle benannten HTML-Zeichen in ihre entsprechenden Ursprungszeichen
get_html_translation_table
10-Apr-2007 05:33
22-Feb-2007 02:49
A lot of quite common characters (or at least not rare, like oelig, euro or minus) are missing from the table unfortunately.
Here are some, if you want to make your translation table more complete and your xml data less error-prone. Not sure why some characters have 2 codes, just use one. Here goes: '''=>''', '−'=>'-', 'ˆ'=>'^', '˜'=>'~', 'Š'=>'Š', '‹'=>'‹', 'Œ'=>'Œ', '‘'=>'‘', '’'=>'’', '“'=>'“', '”'=>'”', '•'=>'•', '–'=>'–', '—'=>'—', '˜'=>'˜', '™'=>'™', 'š'=>'š', '›'=>'›', 'œ'=>'œ', 'Ÿ'=>'Ÿ', 'ÿ'=>'ÿ', 'Œ'=>'Œ', 'œ'=>'œ', 'Š'=>'Š', 'š'=>'š', 'Ÿ'=>'Ÿ', 'ƒ'=>'ƒ', 'ˆ'=>'ˆ', '˜'=>'˜', 'Α'=>'Α', 'Β'=>'Β', 'Γ'=>'Γ', 'Δ'=>'Δ', 'Ε'=>'Ε', 'Ζ'=>'Ζ', 'Η'=>'Η', 'Θ'=>'Θ', 'Ι'=>'Ι', 'Κ'=>'Κ', 'Λ'=>'Λ', 'Μ'=>'Μ', 'Ν'=>'Ν', 'Ξ'=>'Ξ', 'Ο'=>'Ο', 'Π'=>'Π', 'Ρ'=>'Ρ', 'Σ'=>'Σ', 'Τ'=>'Τ', 'Υ'=>'Υ', 'Φ'=>'Φ', 'Χ'=>'Χ', 'Ψ'=>'Ψ', 'Ω'=>'Ω', 'α'=>'α', 'β'=>'β', 'γ'=>'γ', 'δ'=>'δ', 'ε'=>'ε', 'ζ'=>'ζ', 'η'=>'η', 'θ'=>'θ', 'ι'=>'ι', 'κ'=>'κ', 'λ'=>'λ', 'μ'=>'μ', 'ν'=>'ν', 'ξ'=>'ξ', 'ο'=>'ο', 'π'=>'π', 'ρ'=>'ρ', 'ς'=>'ς', 'σ'=>'σ', 'τ'=>'τ', 'υ'=>'υ', 'φ'=>'φ', 'χ'=>'χ', 'ψ'=>'ψ', 'ω'=>'ω', 'ϑ'=>'ϑ', 'ϒ'=>'ϒ', 'ϖ'=>'ϖ', ' '=>' ', ' '=>' ', ' '=>' ', '‌'=>'‌', '‍'=>'‍', '‎'=>'‎', '‏'=>'‏', '–'=>'–', '—'=>'—', '‘'=>'‘', '’'=>'’', '‚'=>'‚', '“'=>'“', '”'=>'”', '„'=>'„', '†'=>'†', '‡'=>'‡', '•'=>'•', '…'=>'…', '‰'=>'‰', '′'=>'′', '″'=>'″', '‹'=>'‹', '›'=>'›', '‾'=>'‾', '⁄'=>'⁄', '€'=>'€'
22-Feb-2007 02:49
and a few more :
'ℑ'=>'ℑ', '℘'=>'℘', 'ℜ'=>'ℜ', '™'=>'™', 'ℵ'=>'ℵ', '←'=>'←', '↑'=>'↑', '→'=>'→', '↓'=>'↓', '↔'=>'↔', '↵'=>'↵', '⇐'=>'⇐', '⇑'=>'⇑', '⇒'=>'⇒', '⇓'=>'⇓', '⇔'=>'⇔', '∀'=>'∀', '∂'=>'∂', '∃'=>'∃', '∅'=>'∅', '∇'=>'∇', '∈'=>'∈', '∉'=>'∉', '∋'=>'∋', '∏'=>'∏', '∑'=>'∑', '−'=>'−', '∗'=>'∗', '√'=>'√', '∝'=>'∝', '∞'=>'∞', '∠'=>'∠', '∧'=>'∧', '∨'=>'∨', '∩'=>'∩', '∪'=>'∪', '∫'=>'∫', '∴'=>'∴', '∼'=>'∼', '≅'=>'≅', '≈'=>'≈', '≠'=>'≠', '≡'=>'≡', '≤'=>'≤', '≥'=>'≥', '⊂'=>'⊂', '⊃'=>'⊃', '⊄'=>'⊄', '⊆'=>'⊆', '⊇'=>'⊇', '⊕'=>'⊕', '⊗'=>'⊗', '⊥'=>'⊥', '⋅'=>'⋅', '⌈'=>'⌈', '⌉'=>'⌉', '⌊'=>'⌊', '⌋'=>'⌋', '⟨'=>'〈', '⟩'=>'〉', '◊'=>'◊', '♠'=>'♠', '♣'=>'♣', '♥'=>'♥', '♦'=>'♦'
31-Dec-2006 08:43
htmlentities includes htmlspecialchars, so here's how to convert an UTF-8 string :
htmlentities($string, ENT_QUOTES, 'UTF-8');
04-Dec-2006 03:31
Another way of converting HTML entities into numeric entities to please XML parsers is using two arrays as conversion tables in a preg_replace function. The conversion table mechanism is based on Ryan's examples above.
<?php
function xmlEntities($s){
//build first an assoc. array with the entities we want to match
$table1 = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
//now build another assoc. array with the entities we want to replace (numeric entities)
foreach ($table1 as $k=>$v){
$table1[$k] = "/$v/";
$c = htmlentities($k,ENT_QUOTES,"UTF-8");
$table2[$c] = "&#".ord($k).";";
}
//now perform a replacement using preg_replace
//each matched value in array 1 will be replaced with the corresponding value in array 2
$s = preg_replace($table1,$table2,$s);
return $s;
}
?>
29-Oct-2006 08:25
There have been issues when hispanic websites or other websites dont use the corrent collision in mysql.
Some problems result that the accents (éä ... ) result in weird characters when a backup is done and restored later on. Or when database is changed to another one.
To fix this try something like this
function accents($text){
foreach(get_html_translation_table(HTML_ENTITIES) as $a=>$b){
$text = str_replace($a,$b,$text);
}
return $text;
}
and use as accents("Hello ....... WITH ACCENTS") and it will return the escaped string.
23-Jul-2006 04:04
Quite disappointingly, get_html_translation_table() only gives the characters for ISO-8859-1, making it quite useless for UTF-8 or anything else like that (as a previous commenter noticed).
30-May-2005 04:00
Not sure what's going on here but I've run into a problem that others might face as well...
<?php
$translations = array_flip(get_html_translation_table(HTML_ENTITIES,ENT_QUOTES));
?>
returns the single quote ' as being equal to ' while
<?php
$translatedString = htmlentities($string,ENT_QUOTES);
?>
returns it as being equal to '
I've had to do a specific string replacement for the time being... Not sure if it's an issue with the function or the array manipulation.
-Pat
19-May-2005 01:30
If you want to display special HTML entities in a web browser, you can use the following code:
<?
$entities = get_html_translation_table(HTML_ENTITIES);
foreach ($entities as $entity) {
$new_entities[$entity] = htmlspecialchars($entity);
}
echo "<pre>";
print_r($new_entities);
echo "</pre>";
?>
If you don't, the key name of each element will appear to be the same as the element content itself, making it look mighty stupid. ;)
26-Jan-2005 11:05
In XML, you can't assume that the doctype will include the same character entity definitions as HTML. XML authors may require character references instead. The following two functions use get_html_translation_table() to encode data in numeric references. The second, optional argument can be used to substitute a different translation table.
function xmlcharacters($string, $trans='') {
$trans=(is_array($trans))? $trans:get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
foreach ($trans as $k=>$v)
$trans[$k]= "&#".ord($k).";";
return strtr($string, $trans);
}
function xml_character_decode($string, $trans='') {
$trans=(is_array($trans))? $trans:get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
foreach ($trans as $k=>$v)
$trans[$k]= "&#".ord($k).";";
$trans=array_flip($trans);
return strtr($string, $trans);
}
03-Jan-2003 03:06
Alans version didn't seem to work right. If you're having the same problem consider using this slightly modified version instead:
function unhtmlentities ($string) {
$trans_tbl = get_html_translation_table (HTML_ENTITIES);
$trans_tbl = array_flip ($trans_tbl);
$ret = strtr ($string, $trans_tbl);
return preg_replace('/&#(\d+);/me',
"chr('\\1')",$ret);
}
04-Jun-2002 07:00
If you want to decode all those { symbols as well....
function unhtmlentities ($string) {
$trans_tbl = get_html_translation_table (HTML_ENTITIES);
$trans_tbl = array_flip ($trans_tbl);
$ret = strtr ($string, $trans_tbl);
return preg_replace('/\&\#([0-9]+)\;/me',
"chr('\\1')",$ret);
}
19-Jun-2001 10:41
get_html_translation_table
It works only with the first 256 Codepositions.
For Higher Positions, for Example ф
(a kyrillic Letter) it shows the same.