Last active
February 11, 2024 01:18
-
-
Save daraul/7057c25495dc0284d1c4e77997d25938 to your computer and use it in GitHub Desktop.
Decode PHP's htmlspecialchars encoding with Javascript
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
return function(text) { | |
var map = { | |
'&': '&', | |
'&': "&", | |
'<': '<', | |
'>': '>', | |
'"': '"', | |
''': "'", | |
'’': "’", | |
'‘': "‘", | |
'–': "–", | |
'—': "—", | |
'…': "…", | |
'”': '”' | |
}; | |
return text.replace(/\&[\w\d\#]{2,5}\;/g, function(m) { return map[m]; }); | |
}; |
Thank you !
Rewrited to typesccript:
const htmlSpecialCharsDecode = (text: string): string => {
const map: Record<string, string> = {
'&': '&',
'&': '&',
' ': ' ',
'<': '<',
'>': '>',
'"': '"',
''': "'",
'’': '’',
'‘': '‘',
'–': '–',
'—': '—',
'…': '…',
'”': '”',
};
return text.replace(/\&[\w\d\#]{2,5}\;/g, (m) => map[m] ?? m);
};
export default htmlSpecialCharsDecode;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!