note 93429 deleted from function.bbcode-create by danbrown
| From: | danbrown@php.net | Date: | Wed, 09 Sep 2009 20:06:51 +0000 |
| Subject: | note 93429 deleted from function.bbcode-create by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160367@lists.php.net to get a copy of this message | ||
Note Submitter: adz at adamhaskell dot net
----
Here's a fully-functioning BBCode engine, using simple regexes.
<?php // BBCode handler
function parse_bbcode( $text) {
$bbcode = Array(
"(\[b\](.*)\[\/b\])" => "<b>$1</b>",
"(\[i\](.*)\[\/i\])" => "<i>$1</i>",
"(\[color=(.+?)\](.*)\[\/color\])" => "<span style='color:
$1;'>$2</span>",
"(\[size=(.+?)\](.*)\[\/size\])" => "<span style='font-size:
$1;'>$2</span>"
[...]
);
$text = str_replace( "<", "<", $text);
$text = str_replace( ">", ">", $text);
$text = nl2br( $text);
foreach( $bbcode as $regex => $replace) {
while( preg_match( $regex, $text)) {
$text = preg_replace( $regex, $replace, $text);
}
}
return $text;
}
?>
This engine assumes quantifiers are greedy by default, and will handle nested tags correctly.
If you have a mySQL database, you can import data from a table into the $bbcode array.