Copy and paste the following code into your editor and run it (you should end up with 2 files: one for the form in html and another one in php).
If you want to see the live result, try it here (new window).
Let's first start building an online form in order to input the html text we wish to encode:
Now comes the function to retrieve the content of the form and encode it (encodehtml.php):
Have a look to the source code to see how the string is coded.