note 93429 added to function.bbcode-create
| From: | adz at adamhaskell dot net | Date: | Wed, 09 Sep 2009 18:25:53 +0000 |
| Subject: | note 93429 added to function.bbcode-create | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160366@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 90.38.135.114
----
Manual Page -- http://www.php.net/manual/en/function.bbcode-create.php
Edit -- https://master.php.net/note/edit/93429
Del: integrated -- https://master.php.net/note/delete/93429/integrated
Del: useless -- https://master.php.net/note/delete/93429/useless
Del: bad code -- https://master.php.net/note/delete/93429/bad+code
Del: spam -- https://master.php.net/note/delete/93429/spam
Del: non-english -- https://master.php.net/note/delete/93429/non-english
Del: in docs -- https://master.php.net/note/delete/93429/in+docs
Del: other reasons-- https://master.php.net/note/delete/93429
Reject -- https://master.php.net/note/reject/93429
Search -- https://master.php.net/manage/user-notes.php