HTML Encode
What is Html Encode tool?
The application supports online html encode, you just need to enter the html source code or the content needs to be encoded
Html encode in Php
htmlentities — Convert all applicable characters to HTML entities
This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.
htmlentities ( string $string , int $flags = ENT_COMPAT , string|null $encoding = null , bool $double_encode = true ) : string
$str = A 'quote' is <b>bold</b>;
// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str);
// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str, ENT_QUOTES);
Example #2 Usage of ENT_IGNORE
$str = "\x8F!!!";
// Outputs an empty string
echo htmlentities($str, ENT_QUOTES, "UTF-8");
// Outputs "!!!"
echo htmlentities($str, ENT_QUOTES | ENT_IGNORE, "UTF-8");
Html encode in Javascript
If you don't want to use external libraries, or don't need too many options when encoding html, then you can use my code below.
Here is the html encoding with javascript short and enough to use. It can be called htmlentities in javascript
(function(window) {
window.htmlentities = {
encode: function(str) {
var buf = [];
for (var i = str.length - 1; i >= 0; i--) {
buf.unshift(['', str[i].charCodeAt(), ';'].join(''));
}
return buf.join('');
},
decode: function(str) {
return str.replace(/(\d+);/g, function(match, dec) {
return String.fromCharCode(dec);
});
}
};
})(window);
htmlentities.encode('<h2>Html encode in Javascript</h2>');
//Output: <h2>Html encode in Javascript</h2>
However, the above function will encode all the characters. If you only want to encode special characters then use the other method below
function htmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
htmlEntities('<h2>Html encode in Javascript</h2>')
//Output: <h2>Html encode in Javascript</h2>