 |
GlobalWeb.com.ru Free Web Hosting :: Commercial Web Hosting :: PHP/mySQL :: CGI/Perl
|
|
|
| Author |
Message |
ReV[a]N Guest
|
Posted: Fri Feb 27, 2004 1:46 pm Post subject: Making Your Own BBCode |
|
|
Your reading this and asking yourself where would I ever use this? Well many places, like if you where making your own forum, getting data from a forums (like phpbb) database to put in yours news section of your website. Well whatever the reason(s) are here it is. Simple and easy. Well here we go. . .
First you want to make an array for patterns you want to parse into actuall HTML.
| Code: | /* Sets what to Search For */
$patterns = array(
"quote"=>"#\\[quote\\](.*?)\\[/quote\\]#si",
"quote_parm"=>"#\\[quote=\"(.*?)\"\\](.*?)\\[/quote\\]#si",
"url"=>"#\\[url=(.*?)\\](.*?)\\[/url\\]#si",
"u"=>"#\\[u\\](.*?)\\[/u\\]#si",
"i"=>"#\\[i\\](.*?)\\[/i\\]#si",
"b"=>"#\\[b\\](.*?)\\[/b\\]#si",
"list"=>"#\\[list\\](.*?)\\[/list\\]#si",
"img"=>"#\\[img\\](.*?)\\[/img\\]#si",
"color"=>"#\\[color=(.*?)\\](.*?)\\[/color\\]#si",
"size"=>"#\\[size=(.*?)\\](.*?)\\[/size\\]#si",
); |
Notice that you have to put \\ in front of [ and ], I dont know why PHP makes you do this but it does. The (.*?) means match anything, any length. Now you can customize this to however you want. Now lets make the array that contains the replacements, the actuall HTML.
| Code: | /* Sets what to replace it with */
$replacements = array(
"quote=>"<some stuff to put around the quote>\\1</stuff>",
"quote_parm"=>"\\1 Wrote: \\2",
"url"=>"<A HREF=\"\\1\" TARGET=\"_blank\">\\2</A>",
"u"=>"<u>\\1</u>",
"i"=>"<i>\\1</i>",
"b"=>"<b>\\1</b>",
"list"=>"<ul>\\1</ul>",
"img"=>"<img src=\"\\1\">",
"color"=>"<font color=\"\\1\">\\2</font>",
"size"=>"<span style=\"font-size:\\1px;\">\\2</span>"
); |
The code around the quote tag you will want to replace with your own version of how a quote should look (example: in a table or just bolded or something). Notice the \\1 and \\2's all over the place, thats taking what was in the first (.*?) and putting it where the \\1 is and same for the second, and so on. Nows lets make it actually replace the BBCode with HTML. Dont be alarmed, its quite easy. Here it is.
| Code: | | $text = preg_replace($patterns, $replacements, $text); |
There you go, you just have to put it all together now! Of course you would replace the $text variable with whatever variable you are wanting to parse BBCode into HTML in. Below is all the code put together.
| Code: | /* Sets what to Search For */
$patterns = array(
"quote"=>"#\\[quote\\](.*?)\\[/quote\\]#si",
"quote_parm"=>"#\\[quote=\"(.*?)\"\\](.*?)\\[/quote\\]#si",
"url"=>"#\\[url=(.*?)\\](.*?)\\[/url\\]#si",
"u"=>"#\\[u\\](.*?)\\[/u\\]#si",
"i"=>"#\\[i\\](.*?)\\[/i\\]#si",
"b"=>"#\\[b\\](.*?)\\[/b\\]#si",
"list"=>"#\\[list\\](.*?)\\[/list\\]#si",
"img"=>"#\\[img\\](.*?)\\[/img\\]#si",
"color"=>"#\\[color=(.*?)\\](.*?)\\[/color\\]#si",
"size"=>"#\\[size=(.*?)\\](.*?)\\[/size\\]#si",
);
/* Sets what to replace it with */
$replacements = array(
"quote=>"<some stuff to put around the quote>\\1</stuff>",
"quote_parm"=>"\\1 Wrote: \\2",
"url"=>"<A HREF=\"\\1\" TARGET=\"_blank\">\\2</A>",
"u"=>"<u>\\1</u>",
"i"=>"<i>\\1</i>",
"b"=>"<b>\\1</b>",
"list"=>"<ul>\\1</ul>",
"img"=>"<img src=\"\\1\">",
"color"=>"<font color=\"\\1\">\\2</font>",
"size"=>"<span style=\"font-size:\\1px;\">\\2</span>"
);
$text = preg_replace($patterns, $replacements, $text); |
Enjoy and I hope this helps. |
|
| Back to top |
|
 |
ReV[a]N
Joined: 27 Feb 2004 Posts: 3
|
Posted: Fri Feb 27, 2004 2:03 pm Post subject: |
|
|
I decided to make a update and since I cant edit the post above (because I wasnt logged in when I did it) here it is. (And sorry for my typos above )
| Code: | <?php
/**************************************************
**
** BBCode.php
**
** Make any additions you want To this BBCode
** php document.
**
**************************************************/
/**************************************************
**
** BBCode_Replace(string "$text");
**
** Takes a string and parses out all BBCode
** and returns it as HTML.
**
**************************************************/
function BBCode_Replace($text)
{
/* Sets what to Search For */
$patterns = array(
"quote"=>"#\\[quote\\](.*?)\\[/quote\\]#si",
"quote_parm"=>"#\\[quote=\"(.*?)\"\\](.*?)\\[/quote\\]#si",
"url"=>"#\\[url=(.*?)\\](.*?)\\[/url\\]#si",
"u"=>"#\\[u\\](.*?)\\[/u\\]#si",
"i"=>"#\\[i\\](.*?)\\[/i\\]#si",
"b"=>"#\\[b\\](.*?)\\[/b\\]#si",
"list"=>"#\\[list\\](.*?)\\[/list\\]#si",
"img"=>"#\\[img\\](.*?)\\[/img\\]#si",
"color"=>"#\\[color=(.*?)\\](.*?)\\[/color\\]#si",
"size"=>"#\\[size=(.*?)\\](.*?)\\[/size\\]#si",
);
/* Sets what to replace it with */
$replacements = array(
"quote=>"<b>Quote: <br> \\1</b>",
"quote_parm"=>"<b>\\1 Wrote: <br> \\2</b>",
"url"=>"<A HREF=\"\\1\" TARGET=\"_blank\">\\2</A>",
"u"=>"<u>\\1</u>",
"i"=>"<i>\\1</i>",
"b"=>"<b>\\1</b>",
"list"=>"<ul>\\1</ul>",
"img"=>"<img src=\"\\1\">",
"color"=>"<font color=\"\\1\">\\2</font>",
"size"=>"<span style=\"font-size:\\1px;\">\\2</span>"
);
$text = preg_replace($patterns, $replacements, $text);
return $text;
}
?> |
|
|
| Back to top |
|
 |
sam Banned
Joined: 01 Mar 2004 Posts: 51 Location: IL
|
Posted: Mon Mar 01, 2004 12:36 pm Post subject: |
|
|
| what is this? |
|
| Back to top |
|
 |
|
|