HTML Encode

Enter HTML to 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 &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str);

// Outputs: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;
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: &#60;&#104;&#50;&#62;&#72;&#116;&#109;&#108;&#32;&#101;&#110;&#99;&#111;&#100;&#101;&#32;&#105;&#110;&#32;&#74;&#97;&#118;&#97;&#115;&#99;&#114;&#105;&#112;&#116;&#60;&#47;&#104;&#50;&#62; 

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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g,  '&quot;');
}

htmlEntities('<h2>Html encode in Javascript</h2>')
//Output: &lt;h2&gt;Html encode in Javascript&lt;/h2&gt;