phpjs
Version:
106 lines (89 loc) • 3.38 kB
Markdown
layout: page
title: "JavaScript htmlentities function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/htmlentities:425
- /functions/view/htmlentities
- /functions/view/425
- /functions/htmlentities:425
- /functions/425
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's htmlentities
{% codeblock strings/htmlentities.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/htmlentities.js raw on github %}
function htmlentities (string, quote_style, charset, double_encode) {
// From: http://phpjs.org/functions
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: nobbler
// + tweaked by: Jack
// + bugfixed by: Onno Marsman
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + input by: Ratheous
// + improved by: Rafał Kukawski (http://blog.kukawski.pl)
// + improved by: Dj (http://phpjs.org/functions/htmlentities:425#comment_134018)
// - depends on: get_html_translation_table
// * example 1: htmlentities('Kevin & van Zonneveld');
// * returns 1: 'Kevin & van Zonneveld'
// * example 2: htmlentities("foo'bar","ENT_QUOTES");
// * returns 2: 'foo'bar'
var hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style),
symbol = '';
string = string == null ? '' : string + '';
if (!hash_map) {
return false;
}
if (quote_style && quote_style === 'ENT_QUOTES') {
hash_map["'"] = ''';
}
if (!!double_encode || double_encode == null) {
for (symbol in hash_map) {
if (hash_map.hasOwnProperty(symbol)) {
string = string.split(symbol).join(hash_map[symbol]);
}
}
} else {
string = string.replace(/([\s\S]*?)(&(?:#\d+|#x[\da-f]+|[a-zA-Z][\da-z]*);|$)/g, function (ignore, text, entity) {
for (symbol in hash_map) {
if (hash_map.hasOwnProperty(symbol)) {
text = text.split(symbol).join(hash_map[symbol]);
}
}
return text + entity;
});
}
return string;
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/htmlentities.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/htmlentities.js)
### Example 1
This code
{% codeblock lang:js example %}
htmlentities('Kevin & van Zonneveld');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'Kevin & van Zonneveld'
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
htmlentities("foo'bar","ENT_QUOTES");
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'foo'bar'
{% endcodeblock %}
### Other PHP functions in the strings extension
{% render_partial _includes/custom/strings.html %}