UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

52 lines (46 loc) 1.73 kB
function htmlentities(string, quote_style, charset, double_encode) { // discuss at: http://phpjs.org/functions/htmlentities/ // original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // improved by: nobbler // improved by: Jack // improved by: Rafał Kukawski (http://blog.kukawski.pl) // improved by: Dj (http://phpjs.org/functions/htmlentities:425#comment_134018) // bugfixed by: Onno Marsman // bugfixed by: Brett Zamir (http://brett-zamir.me) // input by: Ratheous // 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; }