-
-
Save batazo/5677c869e851ddc70c44e55691ab90fd to your computer and use it in GitHub Desktop.
Simple BBcode rendering function (PHP).
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 | |
/** | |
* Renders BBCode to html, suitable to be embedded in a document. Supports only | |
* a limited set of BBCode tags (can be extended), which are: | |
* | |
* @param $bbcode The bbcode to be rendered. Must not be `null` or empty | |
* string. | |
* @param $reps Optional additional replacements. Allows you to add custom tags | |
* etc. Should be an associative array, mapping a regex (preg) to replacement. | |
* For example: `array('{\[b\](.+?)\[/b\]}is' => "<b>$1</b>\n")` adds the | |
* [b] tag. | |
*/ | |
function bbcode($bbcode, $reps = array()) { | |
assert($bbcode != null && $bbcode != ''); | |
// first encode any HTML special charaters | |
$bbcode = htmlentities($bbcode); | |
// ensure paragraph structure | |
$bbcode = "<p>$bbcode</p>"; | |
// single lines to paragraph break | |
$bbcode = preg_replace('/^\s*$/m', '</p><p>', $bbcode); | |
// add built-in regex replacements to perform (i.e. core bbcode tags) | |
$reps['{\[b\](.+?)\[/b\]}is'] = "<b>$1</b>\n"; // [b] - bold | |
$reps['{\[i\](.+?)\[/i\]}is'] = "<i>$1</i>\n"; // [i] - italic | |
$reps['{\[u\](.+?)\[/u\]}is'] = "<u>$1</u>\n"; // [u] - underline | |
$reps['{\[s\](.+?)\[/s\]}is'] = "<s>$1</s>\n"; // [s] - strikethrough | |
$reps['{\[color=(.+?)\](.*?)\[/color\]}is'] = "<span style='color:$1;'>$2</span>\n"; // [color] | |
$reps['{\[quote\](.*?)\[/quote\]}is'] = "</p>\n<blockquote><p>\n$1\n</p></blockquote>\n<p>\n"; // [quote] | |
$reps['{\[code\](.*?)\[/code\]}is'] = "<pre>$1</pre>\n"; // [code] | |
$reps['{\[url\](.+?)\[/url\]}is'] = "<a href='$1'>$1</a>\n"; // [url] - raw url | |
$reps['{\[url=(.*?)\](.+?)\[/url\]}is'] = "<a href='$1'>$2</a>\n"; // [url=] - with text | |
$reps['{\[img\](.*?)\[/img\]}is'] = "<img src='$1' alt=''/>\n"; // [img] | |
$reps['{\[ul\](.*?)\[/ul\]}is'] = "</p>\n<ul>$1</ul>\n<p>\n"; // [ul] - unordered list | |
$reps['{\[ol\](.*?)\[/ol\]}is'] = "</p>\n<ol>$1</ol>\n<p>\n"; // [ol] - ordered list | |
$reps['{\[\*\](.*?)\[/\*\]}is'] = "<li>\n<p>\n$1\n</p>\n</li>\n"; // [*] - list item | |
// perform replacements | |
foreach ($reps as $regex => $replace) | |
$bbcode = preg_replace($regex, $replace, $bbcode); | |
return $bbcode; | |
} | |
?> |
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_once dirname(__FILE__).'/code/bbcode.php'; | |
$bbcode = isset($_POST['bbcode']) ? | |
$_POST['bbcode']: | |
'[quote]Write some [b]bbcode[/b] here![/quote]'; | |
?> | |
<!DOCTYPE HTML> | |
<html lang="en-US"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<h1>BBCode Test</h1> | |
<form action="bbcode.php" method="post"> | |
<label for="bbcode">BBCode:</label> | |
<textarea name="bbcode" cols="30" rows="10"><?php echo $bbcode ?></textarea> | |
<input type="submit" value="Preview"/> | |
</form> | |
<h2>Output</h2> | |
<?php | |
if (isset($bbcode)) | |
echo bbcode($bbcode); | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment