allow html tags in textboxes to submit

Posted on 2002-05-26
Medium Priority
Last Modified: 2012-05-04
i've been 'messing' around with several functions (htmlsspecialchars, strip_tags,...) but can not get to work as it should...

if a user enters html tags, e.g.
<input type="text" value="some value" name="somename">

then presses submit, i want this text to be added to the database,
and when it is viewed again,
it either shows a real textbox (NOT good) or it shows nothing at all (even worse), depending on the functions i use.

also, when a user enters a url or an email address, can this be show as a clickable url or email address?

i defenitly need some good advice on all procautions to write to a db, and to retrieve the data again.
i also use nl2br to cleanup the /n's into <br> tags, only when retrieving data.

300 for the one who can make this clear so i can get this into my head...

Question by:Paurths
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4

Accepted Solution

dkjariwala earned 1200 total points
ID: 7035077
First thing.
What you want to do ?

Do you want to allow ppl to use HTML tags ?
If your answer is NO,

Then just use htmlentities to your variable so that when you store the data in db and retrieve back, it would be displayed exactly as person typed rather than
some messy html.

so lets say your input name is 'somename'

get value in variable by

$somename = htmlentities($HTTP_POST_VARS['somename']); //assuming form is submitted using POST.

Now if person has typed,

<a href="mailto:me@me.com">mail me</a>

Then it would get stored as

&lt;a href=&quot;mailto:me@me.com&quot;&gt;mail me&lt;/a&gt;

in variable $somename.

So no person can put in some HTM and screw your output.

That is good. But, sometimes you would like person to use some tags but block others.
Say use of <b> tag is not at all harmful. so you would like to allow it.

In that case use,

strip_tags, like

$somename = strip_tags($somename,'<b>');

so $somename would STRIP all tags except <b> !! Mind well, here you would be actually loosing information person typed.
Lets say person used img tag, if you use strip_tags, all that will be removed. ( where in htmlentities it is converted so that it gets displayed instead of being rendered by browser.)

Now you would like to convert URL to clickable one.

Here you would have to write few regexps.

Check this,

$somename =eregi_replace("([ \r\n])www\\.([^ ,\r\n]*)","<a href=\"http://www.\\2\">\\2</a>",$somename);
$somename =eregi_replace("([ \r\n])http://([^ ,\r\n]*)","<a href=\"http://\\2\">\\2</a>",$somename);
$somename =eregi_replace("([ \r\n])https://([^ ,\r\n]*)","<a href=\"https://\\2\">\\2</a>",$somename);
$somename =eregi_replace("([ \r\n])ftp://([^ ,\r\n]*)","<a href=\"ftp://\\2\">\\2</a>",$somename);
$somename =eregi_replace("^http://([^ ,\r\n]*)","<a href=\"http://\\1\">\\1</a>",$somename);
$somename =eregi_replace("^https://([^ ,\r\n]*)","<a href=\"https://\\1\">\\1</a>",$somename);
$somename=eregi_replace("^ftp://([^ ,\r\n]*)","<a href=\"ftp://\\1\">\\1</a>",$somename);
$somename=eregi_replace("^www\\.([^ ,\r\n]*)","<a href=\"http://www.\\1\">\\1</a>",$somename);

So this statements takes care of many possiblities person may have typed url and convert in to clickable one.

Now converting new lines to <br>.
Now here you have two options.
1. Either store new lines as <br> itself, or
2.convert new lines to <br> when displaying.

If 1) then you would do
     $someone = nl2br($someone)
     and then store $someone in DB. So all your new lines are permanently replaced by <br> tags.
     This is not much preferable as you might sometime want to send mail using DB data in which case <br> would appear in mail.
if 2) then don't do anything just store $someone in DB.
     When you want to display it,
     Fetch value from DB,
     then do like $display_value = nl2br($someone);
     and output $display_value.

also you should take care of single quote/double quote when you insert data in db.
and it is very easy too.
just do like

if(! get_magic_quotes_gpc())
     $someone = addslashes($someone);

This code takes care that if MAGIC_QUOTES is not on ( which automatically escapes quotes ) , you manually escape the quotes. :)

I hope this makes things clear to you.



Expert Comment

ID: 7035081
Hey Ricky...

I normally leave the data as-is when entering it into the database, although there are exceptions to this rule. In other words, I am referring to the following scenario:

User is entering html tags into a textarea, and submits the form. I will normally insert this data into the database as is (without stripping the html tags and/or nl2br). The reason for this is because my needs for displaying this data may change at a later date, so I would rather have the original content in there and just change my display routine as needed.

How you go about it though is really up to you. If the user is entering html code through the form, and you know for sure that it will never actually be rendered, but rather just displayed as text on the page, then you could just as well pass the string through htmlspecialchars() before inserting.

Another example would be how we have our user pages set up at my work. Our members can create/edit their own web page through a web interface. Obviously, the user should be able to see the html code in the textarea when they are editing it, but the code should actually render on the page when someone goes to their web page. Since the web page will normally get many more hits than the user's edit page, we save the html tags into the database, rather than convert them before inserting into the db. The reason for this is because there is more traffic to the user's web page, which in turn would involve more processing on each page if the text had to be converted every time we pulled it out of the db. Instead, it only needs to be converted when we pull it out of the db and put it into the textarea for the owner to edit.

So in reality, the method you use is dependant on the circumstances. Whether converting characters before inserting into the database, or pulling the original characters from the db and converting them before displaying; whichever happens to be more convenient for the situation.

If you have any specific questions regarding certain functions, feel free to let me know. I am about to go to bed, so I will check back in the morning to see if you have any questions.

LVL 12

Author Comment

ID: 7035120
wow, thanks for this response.

it is starting to get clear now ( a little hehe)

so, JD,

suppose i would like to let the user use:

and the hyperlinks

but nothing else.

do i first use htmlentities to make sure textboxes, tables etc are 'dismantled', and then use str_replace to get the font-tags back in?
//strip tags
$txtcomment = htmlentities($HTTP_POST_VARS['txtcomment']);

//allow bold, replace with actual tags
$txtcomment = str_replace("$lt;b&gt;","<B>",$txtcomment);
     $txtcomment = str_replace("$lt;B&gt;","<B>",$txtcomment);
     $txtcomment = str_replace("$lt;/b&gt;","</B>",$txtcomment);
     $txtcomment = str_replace("$lt;/B&gt;","</B>",$txtcomment);

seems not to be working, it is nicely showing the data the user typed in (which in a way is good ofcourse)
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

LVL 12

Author Comment

ID: 7035123
$txtcomment = str_replace("$lt;b&gt;","<B>",$txtcomment);

is ofcourse (not $ but &)

$txtcomment = str_replace("&lt;b&gt;","<B>",$txtcomment);

Expert Comment

ID: 7035127
The following seems to be working for me,


$check = "<a href=\"mailto:me@me.com\">mail me<a> <b> wow....</b>";

$check = htmlentities($check);

$check =str_replace("&lt;b&gt;","<b>",$check);
$check =str_replace("&lt;/b&gt;","</b>",$check);
print $check;

LVL 12

Author Comment

ID: 7035129
it is working when i use str_replace on outputting the data again. (B, U and I)

this is excellent,

will test the links now, and re-comment

LVL 12

Author Comment

ID: 7035142
links are working as well.

email seems to trouble me...

getting close :-)
LVL 12

Author Comment

ID: 7035143
this is what i produced so far for email

$msg=eregi_replace("mailto:([^ ,\r\n]*)","<a href=\"mailto:\\1\">\\1</a>",$msg);

will only work if person enters this in textbox:

strange result with
<a href="mailto:someaddress@domain.com">click me</a>

what is clickable: someaddress@domain.com">click


Expert Comment

ID: 7035151
Hello Ricky,

See there can not be 100% sure way to get everything corrected. You would have to draw line somewhere. And You should try to match up eamil  address by string which has '@' in it rather than looking for mailto: .

LVL 12

Author Comment

ID: 7035157
thanx JD,

this helped me out quite a bit.
I decided emails are not allowed :-)

however, this gave me some idea's on other features,
adding smileys etc,

i changed the code and added a 'help' page, on how ot use these tags.
Instead of <B>bold</B> will show bold, it will show as it is.
If a user however types in: [B]this is bold[/B] then 'this is bold' will show as bold.
smileys are my next step hehe...

thanx alot,


Expert Comment

ID: 7035159
Smileys are also not difficult. Simple str_replace does the task.

$text = str_replace(":)","<img src="smiley.gif">",$text);

So :) would be replaced by image of smily !! simple, isnt it ??

LVL 12

Author Comment

ID: 7035160
yep, its already working


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question