Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save janmyszkier/8f4b729c52c85d5ec866a69cbd46a474 to your computer and use it in GitHub Desktop.
Save janmyszkier/8f4b729c52c85d5ec866a69cbd46a474 to your computer and use it in GitHub Desktop.
Correct double encoded string iso-8859-1 / utf-8
function correctDoubleEncodedString($string) {
// Fix common sequences manually if needed
$decodedString = str_replace(
[
'’', // ’ (right single quotation mark)
'“', // “ (left double quotation mark)
'�', // ” (right double quotation mark)
'–', // – (en dash)
'â€â€�', // — (em dash)
'…', // … (ellipsis)
'ö', // ö
'ç', // ç
'á', // á
'é', // é
'ó', // ó
'ú', // ú
'ã', // ã
'ê', // ê
'ü', // ü
'Û', // û
'č', // č
'ÄŽ', // Ď
'Äš', // Š
'Å¡', // š
'ž', // ž
'Ž', // Ž
'ÄŒ', // Č
'Ď', // Ď
'Ä™', // ę
'Ę', // Ę
'červené', // červené
'Velké', // Velké
'“', // “ (left double quotation mark)
'”', // ” (right double quotation mark)
'', // BOM (Byte Order Mark) or similar encoding issues
'é', // é
'‘', // ‘ (left single quotation mark)
'’', // ’ (right single quotation mark)
'ä', // ä
'′', // ² (superscript two)
'Ó', // Ô (Latin capital letter O with circumflex)
'ã', // ã (Latin small letter a with tilde)
'ÄŒ', // Č
'á', // á
'í', // í
],
[
'’',
'“',
'”',
'–',
'—',
'…',
'ö',
'ç',
'á',
'é',
'ó',
'ú',
'ã',
'ê',
'ü',
'û',
'č',
'Ď',
'Š',
'š',
'ž',
'Ž',
'Č',
'Ď',
'ę',
'Ę',
'červené',
'Velké',
'“',
'”',
'', // Remove BOM if present
'é', // é
'‘', // ‘ (left single quotation mark)
'’', // ’ (right single quotation mark)
'ä', // ä
'²', // ² (superscript two)
'Ô', // Ô (Latin capital letter O with circumflex)
'ã', // ã (Latin small letter a with tilde)
'Č', // Č
'á', // á
'í', // í
],
$string
);
return $decodedString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment