[SOLVED] BBCode!
Moderator: General Moderators
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
[SOLVED] BBCode!
Hello everyone. I'm writing a pretty simple forum that integrates with my user-driven website (hence the reason for not using something like phpBB). I need a function that will convert bbcode to normal HTML (simple bbcode, no lists or any of that). I only need bold, italics, underline, links, and images. Possibly font size. I have no idea where to start, I've never been good with regex. If someone can point me int he right direction, I'll really appreciate it.
Last edited by evilmonkey on Wed Feb 22, 2006 12:30 pm, edited 1 time in total.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Yeah, there's tons of stuff already out there that's why jcart suggested you search google.. for example here's something I wrote
Code: Select all
<?php
class bbcode {
var $text;
function bbcode($text) {
$this -> text = $text;
}
function output() {
echo($this->parse());
}
function parse() {
$this-> text = $this -> formatting($this ->text);
$this-> text = $this -> links($this ->text);
return(nl2br($this->text));
}
function formatting($text) {
$text = preg_replace('@\[b\](.*?)\[/b\]@ims', '<strong>\\1</strong>', $text);
$text = preg_replace('@\[i\](.*?)\[/i\]@ims', '<i>\\1</i>', $text);
$text = preg_replace('@\[u\](.*?)\[/u\]@ims', '<u>\\1</u>', $text);
return($text);
}
function links($text) {
$text = preg_replace('@\[url=(.*?)\](.*?)\[/url\]@ims', '<a href="\\1">\\2</a>', $text);
return($text);
}
}
?>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
I'm posting my bbcode function (takes care of the [quote] problem too) in hopes tha someone can learn from it. Thanks for your help everyone.
Code: Select all
function bbcode($text) {
//bold
$text = preg_replace('@\[b\](.*?)\[/b\]@ims', '<strong>\\1</strong>', $text);
//italics
$text = preg_replace('@\[i\](.*?)\[/i\]@ims', '<i>\\1</i>', $text);
//underline
$text = preg_replace('@\[u\](.*?)\[/u\]@ims', '<u>\\1</u>', $text);
//links
$text = preg_replace('@\[url=(.*?)\](.*?)\[/url\]@ims', '<a href="\\1">\\2</a>', $text);
//images
$text = preg_replace('@\[img\](.*?)\[/img\]@ims', '<img src="\\1">', $text);
//quoting - with a source
while (strpos($text, "[quote=")!== FALSE){
$text = preg_replace('@\[quote=(.*?)\](.*?)\[/quote\]@ims', '<table>
<tr>
<td> </td>
<td>Quote:\\1</td>
</tr>
<tr>
<td> </td>
<td>\\2<br>
</td>
</tr>
</table>', $text);
}
//quoting - no source
while (strpos($text, "[quote]")!==FALSE){
$text = preg_replace('@\[quote\](.*?)\[/quote\]@ims', '<table>
<tr>
<td> </td>
<td>Quote:</td>
</tr>
<tr>
<td> </td>
<td>\\1<br>
</td>
</tr>
</table>', $text);, $text);
}
//line breaks
$text = nl2br($text);
return($text);
}- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
How would you? I made it to find exact matches of the string [quote] and [quote= (without the ending bracket). The only way I can see an infinite loop happenning is if some moron...pardon me...user decides to copy and paste the string [quote] many times. Even then, it's finite. If you see another issue, let me know so I can correct it. 
Thanks.
Thanks.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
How is it finite? Users submit bad tags all the time. Btw, both these will create infinite loops:
Code: Select all
[quote]Code: Select all
[quote=Code: Select all
[quote=[/quote]- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
fyed brought up a good point. I changed the condition of my loops:
Thanks. 
Code: Select all
while (strpos($text, "[quote]")!==FALSE && strpos($text, "[/quote]"!==FALSE)){
//...
}
while (strpos($text, "[quote=")!== FALSE && strpos($text, "[/quote]"!==FALSE)){
//...
}- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
I don't think do while is good for this case...why run the memory intensive regex function if there's nojshpro2 wrote:Yeah your while loop should be checking for the same text it is going to replace (regex), also this is a good place for a do..while loop
while loop for simple quote
check string
replace text
check string
do..while loop for simple quote
replace text
check string
tag at all?