Skip to content

Instantly share code, notes, and snippets.

@daveh
Last active September 7, 2022 02:28
Show Gist options
  • Save daveh/b93ca07a2ce263bdb50d082e86d224cc to your computer and use it in GitHub Desktop.
Save daveh/b93ca07a2ce263bdb50d082e86d224cc to your computer and use it in GitHub Desktop.
Rich-text formatting in PHP: HTML, Markdown, rich-text editors like TinyMCE and doing it securely (code to accompany https://youtu.be/Udgi43MG0a4)
<?php
require "vendor/autoload.php";
$parser = new Parsedown;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Formatting text</title>
</head>
<body>
<h1>Formatting text</h1>
<form method="post">
<div>
<textarea name="content"></textarea>
</div>
<div>
<button>Send</button>
</div>
</form>
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST'): ?>
<div><?= $_POST['content'] ?></div>
<div><?= $parser->text($_POST['content']) ?></div>
<?php endif; ?>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Formatting text</title>
</head>
<body>
<h1>Formatting text</h1>
<form method="post">
<div>
<textarea name="content"></textarea>
</div>
<div>
<button>Send</button>
</div>
</form>
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST'): ?>
<div><?= $_POST['content'] ?></div>
<?php endif; ?>
</body>
</html>
<?php
require "vendor/autoload.php";
$config = HTMLPurifier_Config::createDefault();
$config->set('Cache.DefinitionImpl', null);
$config->set('HTML.AllowedElements', 'strong,em');
$config->set('HTML.AllowedAttributes', []);
$purifier = new HTMLPurifier($config);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Formatting text</title>
<script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script>
<script>tinymce.init({
selector:'textarea',
menubar: false,
plugins: 'code',
toolbar: 'bold italic code'
});</script>
</head>
<body>
<h1>Formatting text</h1>
<form method="post">
<div>
<textarea name="content"></textarea>
</div>
<div>
<button>Send</button>
</div>
</form>
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST'): ?>
<div><?= $purifier->purify($_POST['content']) ?></div>
<?php endif; ?>
</body>
</html>
@pingpong00
Copy link

  • กฟไดดฟไดฟไ
  • ้พกห้่พก่
  • ่ะด่หะป่หะา
  • เดหผกเฟำ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment