Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

laraDev98's avatar

What's the right way to save HTML in database?

I'm currently making an app where the users can enter raw html using wysiwyg editor and after saving that it will be used to create layout for their profile pages in the app and what is the right way of storing this in database?

Do I need to use HTML::entities(Input::get('description'));

Help needed!

0 likes
12 replies
bashy's avatar

Well if you're using one of those editors, you need to save it as is. You then display it either with entities/escape it if you don't want it to be displayed as rending HTML. Be aware that they can input any HTML/JS etc in this case...

laraDev98's avatar

So what's the alternative? How do I stop users from vandalizing my app like

<script>alert("Hello World");</script>
sitesense's avatar
Level 19

You might want to run the input through HTMLPurifier before saving to the database.

https://github.com/mewebstudio/Purifier

It will allow only certain tags in your html, stripping out any nasty things like javascript.

You could use it on the output instead, but that's overhead you can do without on every page view.

2 likes
bashy's avatar

Yeah as sitesense said, use something like that. Just depends who's adding the content in, if it's a client's website you'd probably want to allow most stuff but disallow things that could break the website (flash etc)

andy's avatar

( posting as note to remember this )

Pendo's avatar

Sorry to dig up this old thread. But I was just wondering what place makes the most sense to put the code? Would I add this to a

setNameAttribute($value) {
    return Purifier::clean( $value );
}

Or would I add it in my controller right before I call the save method?

1 like
Pendo's avatar

Thanks, at least that's an answer after 4 months, haha!

Please or to participate in this conversation.