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;