Skip to content

Instantly share code, notes, and snippets.

@reedycat
Forked from daraul/decodeHtmlspecialChars.js
Created December 23, 2020 09:30
Show Gist options
  • Save reedycat/01a06607e4c49e03462f14b3a721ea98 to your computer and use it in GitHub Desktop.
Save reedycat/01a06607e4c49e03462f14b3a721ea98 to your computer and use it in GitHub Desktop.
Decode PHP's htmlspecialchars encoding with Javascript
return function(text) {
var map = {
'&': '&',
'&': "&",
'&lt;': '<',
'&gt;': '>',
'&quot;': '"',
'&#039;': "'",
'&#8217;': "’",
'&#8216;': "‘",
'&#8211;': "–",
'&#8212;': "—",
'&#8230;': "…",
'&#8221;': '”'
};
return text.replace(/\&[\w\d\#]{2,5}\;/g, function(m) { return map[m]; });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment