Created
July 21, 2024 08:43
-
-
Save janmyszkier/8f4b729c52c85d5ec866a69cbd46a474 to your computer and use it in GitHub Desktop.
Correct double encoded string iso-8859-1 / utf-8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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