Jump to content

Recommended Posts

I have written a simple form that stores comments in a database, and displays them on a website where relevant. As such, I have a simple input box for the comment. I was wondering if there is a function or script that will automatically detect and remove HTML tags, so that only text is being saved to the database, and not HTML.

 

As it stands if someone was to put this in the comments section and submit:

 

"I have a cool <a href="http://www.dirtyporn.com">website</a> to share."

 

It would actually display:

 

"I have a cool website to share."

 

And the link would be live. :( Oh noes, right?

Link to comment
https://forums.phpfreaks.com/topic/274327-remove-html-tags/
Share on other sites

The proper way in most circumstances, is not to strip the tags, but to escape the output. That way all characters that ha a special meaning to HTML would be transformed, so that they display as actual text instead.

For HTML output escaping you should use the htmlspecialchars () function.

 

strip_tags () has the problem of not only removing valid HTML tags, but also everything that looks like it might be HTML. So if anyone started their post with "<3" everything would be removed. Or at least everything up to, and including, the first ">".

Link to comment
https://forums.phpfreaks.com/topic/274327-remove-html-tags/#findComment-1411702
Share on other sites

Thanks Christian. I have decided to use strip_tags() because I actually want all of the nonsense between the < and > characters gone. I understand that this poses a problem if the user uses one of those within the text, but unless there is a way to determine whether these characters are being used for HTML purposes or not, its something I will have to risk. I've made this comments script pretty public, and I expect there to be a point where I will not be able to administer and regulate all comments myself, and someone might start to post spam using this form -- big no-no for me.

 

Cheers.

Link to comment
https://forums.phpfreaks.com/topic/274327-remove-html-tags/#findComment-1411913
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.