phpjs
Version:
98 lines (82 loc) • 3.12 kB
Markdown
layout: page
title: "JavaScript html_entity_decode function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/html_entity_decode:424
- /functions/view/html_entity_decode
- /functions/view/424
- /functions/html_entity_decode:424
- /functions/424
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's html_entity_decode
{% codeblock strings/html_entity_decode.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/html_entity_decode.js raw on github %}
function html_entity_decode (string, quote_style) {
// From: http://phpjs.org/functions
// + original by: john (http://www.jd-tech.net)
// + input by: ger
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Onno Marsman
// + improved by: marc andreu
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Ratheous
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + input by: Nick Kolosov (http://sammy.ru)
// + bugfixed by: Fox
// - depends on: get_html_translation_table
// * example 1: html_entity_decode('Kevin & van Zonneveld');
// * returns 1: 'Kevin & van Zonneveld'
// * example 2: html_entity_decode('&lt;');
// * returns 2: '<'
var hash_map = {},
symbol = '',
tmp_str = '',
entity = '';
tmp_str = string.toString();
if (false === (hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style))) {
return false;
}
// fix & problem
// http://phpjs.org/functions/get_html_translation_table:416#comment_97660
delete(hash_map['&']);
hash_map['&'] = '&';
for (symbol in hash_map) {
entity = hash_map[symbol];
tmp_str = tmp_str.split(entity).join(symbol);
}
tmp_str = tmp_str.split(''').join("'");
return tmp_str;
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/html_entity_decode.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/strings/html_entity_decode.js)
### Example 1
This code
{% codeblock lang:js example %}
html_entity_decode('Kevin & van Zonneveld');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'Kevin & van Zonneveld'
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
html_entity_decode('&lt;');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'<'
{% endcodeblock %}
### Other PHP functions in the strings extension
{% render_partial _includes/custom/strings.html %}