26.06.2017, 12:25
Hallo @all,
ich beziehe mich hier auf
http://www.php-rocks.de/thema/142;775-re-session-files-werden-nicht-beschrieben.html#pid775
http://www.php-rocks.de/thema/98;506-die-utf8-verschw-rung.html#pid506
welche ich gerne ergänzen möchte, bzw. ich versuche es so gut wie geht zu reproduzieren.
(sorry, @Arne, früher hab ich es nicht geschafft, beschäftigt)
Problemstellung:
- Ich habe eine Datenbank mit "gemixten" Kollationen und entsprechenden Inhalten.
Lösung:
- Ich analysiere den Output (auf UTF-8) und passe ihn ggf. entsprechend an.
-----------------------------------------------------------------------------------
Referenz:
ob_start
mb_detect_encoding
htmlentities
html_entity_decode
htmlspecialchars_decode
simple_html_dom_parser http://simplehtmldom.sourceforge.net/
-----------------------------------------------------------------------------------
- ob_start startet den Outputbuffer
- simple_html_dom_parser -> find('text') findet Textnodes
http://simplehtmldom.sourceforge.net/
- $isUTF8/mb_detect_encoding rät das Encoding
- $convHtmlEntities kovertiert
- htmlentities konvertiert
-----------------------------------------------------------------------------------
Hinweis:
Je nach Implementation kann das parsen mit simple_html_dom_parser sehr viel CPU verbrauchen!
-----------------------------------------------------------------------------------
Meine konkrete Implementation ist ziemlich "webfan basiert" und nicht allgemein verwendbar deshalb poste ich sie hier nicht, schicke sie aber gerne auf Anfrage per PM o.ä.
mfg
Till
ich beziehe mich hier auf
http://www.php-rocks.de/thema/142;775-re-session-files-werden-nicht-beschrieben.html#pid775
http://www.php-rocks.de/thema/98;506-die-utf8-verschw-rung.html#pid506
welche ich gerne ergänzen möchte, bzw. ich versuche es so gut wie geht zu reproduzieren.
(sorry, @Arne, früher hab ich es nicht geschafft, beschäftigt)
Problemstellung:
- Ich habe eine Datenbank mit "gemixten" Kollationen und entsprechenden Inhalten.
Lösung:
- Ich analysiere den Output (auf UTF-8) und passe ihn ggf. entsprechend an.
-----------------------------------------------------------------------------------
Referenz:
ob_start
mb_detect_encoding
htmlentities
html_entity_decode
htmlspecialchars_decode
PHP-Code:
$isUTF8 = function($string)
{
return preg_match('%(?:
[\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
|\xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
|\xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
|\xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
|[\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
|\xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)+%xs', $string);
};
simple_html_dom_parser http://simplehtmldom.sourceforge.net/
PHP-Code:
$convHtmlEntities = function($str) use($contentTypes, $isUTF8){
if(true !== $isUTF8($str) ){
$map = array(
chr(0x8A) => chr(0xA9),
chr(0x8C) => chr(0xA6),
chr(0x8D) => chr(0xAB),
chr(0x8E) => chr(0xAE),
chr(0x8F) => chr(0xAC),
chr(0x9C) => chr(0xB6),
chr(0x9D) => chr(0xBB),
chr(0xA1) => chr(0xB7),
chr(0xA5) => chr(0xA1),
chr(0xBC) => chr(0xA5),
chr(0x9F) => chr(0xBC),
chr(0xB9) => chr(0xB1),
chr(0x9A) => chr(0xB9),
chr(0xBE) => chr(0xB5),
chr(0x9E) => chr(0xBE),
chr(0x80) => '€',
chr(0x82) => '‚',
chr(0x84) => '„',
chr(0x85) => '…',
chr(0x86) => '†',
chr(0x87) => '‡',
chr(0x89) => '‰',
chr(0x8B) => '‹',
chr(0x91) => '‘',
chr(0x92) => '’',
chr(0x93) => '“',
chr(0x94) => '”',
chr(0x95) => '•',
chr(0x96) => '–',
chr(0x97) => '—',
chr(0x99) => '™',
chr(0x9B) => '’',
chr(0xA6) => '¦',
chr(0xA9) => '©',
chr(0xAB) => '«',
chr(0xAE) => '®',
chr(0xB1) => '±',
chr(0xB5) => 'µ',
chr(0xB6) => '¶',
chr(0xB7) => '·',
chr(0xBB) => '»',
chr(0xE4) => 'ä',
chr(0xDC) => 'Ü',
chr(0xFC) => 'ü',
chr(0xD6) => 'Ö',
chr(0xF6) => 'ö',
chr(0xDF) => 'ß',
chr(0x20AC) => '€'
);
$str = strtr($str, $map);
$str=str_replace('å','ü',$str);
}
$charset = mb_detect_encoding($str, ((isset($contentTypes['charset']) ? $contentTypes['charset'] : 'auto')));
return htmlentities(
html_entity_decode(
htmlspecialchars_decode(
$str,ENT_QUOTES),
ENT_QUOTES | ENT_HTML5, $charset),
ENT_QUOTES | ENT_HTML5, $charset, false // !important
);
};
-----------------------------------------------------------------------------------
- ob_start startet den Outputbuffer
- simple_html_dom_parser -> find('text') findet Textnodes
http://simplehtmldom.sourceforge.net/
- $isUTF8/mb_detect_encoding rät das Encoding
- $convHtmlEntities kovertiert
- htmlentities konvertiert
-----------------------------------------------------------------------------------
Hinweis:
Je nach Implementation kann das parsen mit simple_html_dom_parser sehr viel CPU verbrauchen!
-----------------------------------------------------------------------------------
Meine konkrete Implementation ist ziemlich "webfan basiert" und nicht allgemein verwendbar deshalb poste ich sie hier nicht, schicke sie aber gerne auf Anfrage per PM o.ä.
mfg
Till