Martin AJ Martin AJ - 3 months ago 9
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

<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
<a>
will be a link. I don't want that. How can I prevent that happening?

Answer

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.

Example

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

echo strip_tags($string);

Output

Hello, please remove the tags.

Update

What you are actually looking for is htmlspecialchars.

<?php

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

    echo htmlspecialchars($str, ENT_QUOTES);

?>

Output

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