Martin AJ Martin AJ - 2 months ago 4
CSS Question

How should I store HTML tags into database?

I have an input named comment. User writes his comment into it and sends it under a post. Something like StackOverflow's comments:

enter image description here

As you see, there is a

tag into new comment. And it will be stored in the database like this:

enter image description here

And when I fetch it again, it breaks the HTML of my website:

enter image description here

I mean everything after that
will be a link. I don't want that. How can I prevent that happening?


If you wish to store no tag, then use strip_tags and if you still wish to have mark-up then use something like Parsedown.


$string = '<b>Hello,<> please remove the <> tags.</b>';

echo strip_tags($string);


Hello, please remove the tags.


What you are actually looking for is htmlspecialchars.


    $str = 'Hello <a href="">blah</a>.';

    echo htmlspecialchars($str, ENT_QUOTES);



Hello <a href="">blah</a>.