Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
How to use
Install via yarn add locutus and import:
import { htmlentities } from 'locutus/php/strings/htmlentities'.
Or with CommonJS: const { htmlentities } = require('locutus/php/strings/htmlentities')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
#
code
expected result
1
htmlentities('Kevin & van Zonneveld')
'Kevin & van Zonneveld'
2
htmlentities("foo'bar","ENT_QUOTES")
'foo'bar'
Notes
function is compatible with PHP 5.2 and older
Dependencies
This function uses the following Locutus functions:
return source.replace(regex, function (ent) { if (ent.length > 1) { return shouldDoubleEncode ? hashMap['&'] + ent.substring(1) : ent }
return hashMap[ent] ?? ent }) }
// php/strings/get_html_translation_table (Locutus dependency module) functionget_html_translation_table( table: string | number = 'HTML_SPECIALCHARS', quoteStyle: string | number = 'ENT_COMPAT', ): Record<string, string> { // discuss at: https://locutus.io/php/get_html_translation_table/ // original by: Philip Peterson // revised by: Kevin van Zonneveld (https://kvz.io) // bugfixed by: noname // bugfixed by: Alex // bugfixed by: Marco // bugfixed by: madipta // bugfixed by: Brett Zamir (https://brett-zamir.me) // bugfixed by: T.Wild // improved by: KELAN // improved by: Brett Zamir (https://brett-zamir.me) // input by: Frank Forte // input by: Ratheous // note 1: It has been decided that we're not going to add global // note 1: dependencies to Locutus, meaning the constants are not // note 1: real constants, but strings instead. Integers are also supported if someone // note 1: chooses to create the constants themselves. // example 1: get_html_translation_table('HTML_SPECIALCHARS') // returns 1: {'"': '"', '&': '&', '<': '<', '>': '>'}
return source.replace(regex, function (ent: string): string { if (ent.length > 1) { return shouldDoubleEncode ? hashMap['&'] + ent.substring(1) : ent }
return hashMap[ent] ?? ent }) }
// php/strings/get_html_translation_table (Locutus dependency module) functionget_html_translation_table(table = 'HTML_SPECIALCHARS', quoteStyle = 'ENT_COMPAT') { // discuss at: https://locutus.io/php/get_html_translation_table/ // original by: Philip Peterson // revised by: Kevin van Zonneveld (https://kvz.io) // bugfixed by: noname // bugfixed by: Alex // bugfixed by: Marco // bugfixed by: madipta // bugfixed by: Brett Zamir (https://brett-zamir.me) // bugfixed by: T.Wild // improved by: KELAN // improved by: Brett Zamir (https://brett-zamir.me) // input by: Frank Forte // input by: Ratheous // note 1: It has been decided that we're not going to add global // note 1: dependencies to Locutus, meaning the constants are not // note 1: real constants, but strings instead. Integers are also supported if someone // note 1: chooses to create the constants themselves. // example 1: get_html_translation_table('HTML_SPECIALCHARS') // returns 1: {'"': '"', '&': '&', '<': '<', '>': '>'}
return source.replace(regex, function (ent) { if (ent.length > 1) { return shouldDoubleEncode ? hashMap['&'] + ent.substring(1) : ent }
return hashMap[ent] ?? ent }) }
Improve this function
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.