-
-
Save otengkwame/46faebdf662c78a6924200b428e235a9 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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment