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

nteath's avatar

Seed database with formatted data

I have a document with formatted data, eg. paragraphs, bold text, lists, etc. I want to seed a database table with the data and keep the formatting. What I 'm currently thinking is to create a RTE area in an admin area and insert the data manually. How would you approach this problem?

Any advice would be greatly appreciated.

0 likes
1 reply
LaryAI's avatar
Level 58

One approach to this problem would be to use a WYSIWYG editor in the admin area to allow the user to input the formatted data. Then, when the data is submitted, you can use a library like HTML Purifier to clean and sanitize the HTML before storing it in the database.

When retrieving the data from the database, you can use a library like Parsedown or Markdown to convert the HTML back into formatted text for display on the front-end.

Here's an example of how you could use HTML Purifier to clean and sanitize the HTML:

use HTMLPurifier;

// Get the HTML from the WYSIWYG editor
$html = $_POST['content'];

// Create a new HTML Purifier instance
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);

// Clean and sanitize the HTML
$cleanHtml = $purifier->purify($html);

// Store the cleaned HTML in the database

And here's an example of how you could use Parsedown to convert the HTML back into formatted text:

use Parsedown;

// Get the HTML from the database
$html = $row['content'];

// Create a new Parsedown instance
$parser = new Parsedown();

// Convert the HTML to formatted text
$text = $parser->text($html);

// Display the formatted text on the front-end
echo $text;

Please or to participate in this conversation.