PHP htmlentities

Sep 2 2011 12:04 PM SEP 2 2011 12:04 PM
htmlentities with input text boxesPHP | Tutorials

Ever had issues with putting HTML inside of HTML text boxes? This can be a pain as often times you will find different browsers will read it fine, but then some will break it.

In PHP there is a wonderful function called "htmlentities."

This function can be used for numerous things, but one thing I like to use is for input boxes when I want the input to be able to handle HTML.

Because of the structure of an input placing any sort of HTML can break it.

<input name="htmlinput" type="text" />

There are several ways of going about auto filling this box with HTML. One way would be to add slashes to your command.

$str = ".split(" ").join("");";

This circumstance will actually work as it will escape the double quotes. And prevent when we echo in the value into value="" it wont escape when it should be inputting.

I found this other trick to be extremely helpful in this also:

<input name="htmlinput" type="text" value="<? echo htmlentities($val); ?>" />

It converts all the items in here to their HTML equivalents. When posting the value it will actually convert them back to regular standard formatt.

See the test below to see how this works.