10

I am storing html tags like <ul><li>....</li></ul>, etc in the table. I want to retrieve these values as html content. When I retrieve this data I want to show it as HTML content, that is, it should show bullets instead of <ul><li>....</li></ul>.

Code which I am trying:

<?php echo stripslashes($row3['description'])?>

I have even tried htmlentities(), html_entity_decode() but, none have worked.

1
  • Yeah, it shows output as <ul> <li>......</li></ul>. Commented Feb 5, 2016 at 7:00

6 Answers 6

16

You can Use htmlspecialchars_decode function as below :

echo htmlspecialchars_decode(stripslashes($row3['description'])); 

Instead of

 echo stripslashes($row3['description']);

You can know more about function here : http://php.net/manual/en/function.htmlspecialchars-decode.php

Sign up to request clarification or add additional context in comments.

Comments

14

for laravel users just do this

{!! $my_db_ish['my_row'] !!}

instead of

{{ $my_db_ish['my_row'] }}

2 Comments

Any other options? And why?
dunno if there are other options for now, but i know the first will not escape values from the database while the second will auto escape the values from the database.
1

Use htmlspecialchars_decode.

$str = "<ul><li></li></ul>";

echo htmlspecialchars_decode($str);

Hope it will help you :)

Comments

0

The only way to display the html content is to simply echo $row3['description'], however, this leaves you open to vulnerabilities and unless you really trust the data (ie: never) then you should clean it up first. You can try htmlpurifier for this.

Comments

0

Dont call any

stripslashes, htmlentities or html_entity_decode

These are function to prevent HTML code to be processed as HTML Code (It turns it into text). Just Try to echo $row3['description'];. And make sure to end your lines with ;!!

You should also check if you are saving the data the correct way into the MySQL databae (So it's no changed to prevent HTML coding in the database).

Also it isn't very safe to use HTML code that is saved in a MySQL database that a USER is also involved in. He might add his own HTML code with out you wanting him too, so WATCH OUT!!

Comments

0

Use htmlspecialchars_decode() function. Its works fine for me...

Use guideline

Your Text with code

<?php

    $text = " 
    <html>
    <head>
        <title>Page Title</title>
    </head>
    <body>
         Reference site about Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.
    </body>
    </html> ";

    OR

    $text = $row['columnname'];  // Here define your database table column name.

    echo htmlspecialchars_decode($text);  

?>

Output:

Reference site about Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.