Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Php Bbcode, bbcode for your forum/website/news..
Blessed
post Mar 22 2007, 03:25 PM
Post #1


Advanced Member
*******

Group: Members
Posts: 144
Joined: 22-March 07
Member No.: 40,472



A simple BBcode class

CODE
<?php
//    Filename: bbcode.php
//    Version:     0.1
//

class bbcode {

    function code_box($text)
    {
        $output = "<div class=\"code\"><h6>Code</h6>\\1</div>";
    
    return $output;
    
    }

    function quote($text)
    {
        $output = "<blockquote><h6>Quote:</h6>\\1</blockquote>";
    
    return $output;
    
    }
    
    function htmlout($text)
    {
        $text = stripslashes($text);
        $text = htmlspecialchars($text);    
        $text = nl2br($text);
                
        return $text;
    }


    function parse($text)  
    {
        // First: If there isn't a "[" and a "]" in the message, don't bother.
        $text = " " . $text;
        if (! (strpos($text, "[") && strpos($text, "]")) )
        {
            return $text;    
        }
        else
        {
            // de uiteindelijke code
            $text = $this->htmlout($text);
        
            $text = preg_replace("/\\[b\\](.+?)\[\/b\]/is",'<b>\1</b>', $text);
            $text = preg_replace("/\\[i\\](.+?)\[\/i\]/is",'<i>\1</i>', $text);
            $text = preg_replace("/\\[u\\](.+?)\[\/u\]/is",'<u>\1</u>', $text);
            $text = preg_replace("/\[s\](.+?)\[\/s\]/is",'<s>\1</s>', $text);
            
            $text = preg_replace("/\[code\](.+?)\[\/code\]/is","".$this->code_box('\\1')."", $text);
            $text = preg_replace("/\[quote\](.+?)\[\/quote\]/is","".$this->quote('\\1')."", $text);
    
            $text = eregi_replace("\\[img]([^\\[]*)\\[/img\\]","<img src=\"\\1\">",$text);  
            $text = eregi_replace("\\[size([^\\[]*)\\]([^\\[]*)\\[/size\\]","<font size=\"\\1px\">\\2</font>",$text);
            $text = eregi_replace("\\[color=([^\\[]*)\\]([^\\[]*)\\[/color\\]","<font color=\"\\1\">\\2</font>",$text);    
            return $text;
        }    
    }
}

?>

Example

CODE
<?php
include('bbcode.php');
$bbcode = new bbcode;

$my_content = '[b]This is Bold[/b], [s]Strike[/s]';

$bbcode->parse($my_content);
?>


Have fun

if you want me to add more options feel free to ask

This post has been edited by Blessed: Mar 22 2007, 03:41 PM
Go to the top of the page
 
+Quote Post
matak
post Mar 22 2007, 05:40 PM
Post #2


Super Member
*********

Group: Members
Posts: 412
Joined: 4-October 06
From: Psychedelic Realms
Member No.: 31,079



Here's something that i accidentally discovered a while back smile.gif
How to write with bbcode...

CODE
<?php

$mycode = '<?php echo("hello");?> [b]<html>[/b]';

$stvarno = array ("<", ">", "[b]", "[/b]");

$zamjenjeno = array ("&lt;","&gt;", "<b>", "</b>");

$code = str_replace($stvarno, $zamjenjeno, $mycode);

echo ("$code");

?>


Here's the link to the topic.

I see that you are using a bunch of code for that, and i would like to hear advantages of coding that way. Looks like OOP to me. A thing i'll need to learn in near future. Thanks
Go to the top of the page
 
+Quote Post
Blessed
post Mar 22 2007, 09:13 PM
Post #3


Advanced Member
*******

Group: Members
Posts: 144
Joined: 22-March 07
Member No.: 40,472



i this the bcode class is the siplest way
for the beginners

just include the file
Go to the top of the page
 
+Quote Post
Imtay22
post Apr 13 2007, 02:36 PM
Post #4


Super Member
*********

Group: Members
Posts: 286
Joined: 27-January 07
From: Winter is cold here.
Member No.: 37,984
Spam Patrol



Hey this is really cool i could use this on my site sometime! Only if I had a use for it... I see you only included the normal bbcodes. Maybe try in the next version adding some more advanced ones, like centered text, spoiler, and that kind of stuff. Thanks!
Go to the top of the page
 
+Quote Post
matak
post Apr 13 2007, 03:06 PM
Post #5


Super Member
*********

Group: Members
Posts: 412
Joined: 4-October 06
From: Psychedelic Realms
Member No.: 31,079



I tried to output this code of yours but id didnt do nothing with this

CODE
<?php
include('bbcode.php');

$bbcode = new bbcode;

$my_content = '[b]This is Bold[/b], [s]Strike[/s]';


$bbcode->parse($my_content);
?>


What do i need to echo to get $my_content bbcoded!? Sorry i'm a oop newbie
Go to the top of the page
 
+Quote Post
Blessed
post Apr 13 2007, 03:24 PM
Post #6


Advanced Member
*******

Group: Members
Posts: 144
Joined: 22-March 07
Member No.: 40,472



Try this

echo $bbcode->parse($my_content);


This post has been edited by Blessed: Apr 13 2007, 03:25 PM
Go to the top of the page
 
+Quote Post
matak
post Apr 13 2007, 03:42 PM
Post #7


Super Member
*********

Group: Members
Posts: 412
Joined: 4-October 06
From: Psychedelic Realms
Member No.: 31,079



QUOTE(Blessed @ Apr 13 2007, 05:24 PM) *
Try this

echo $bbcode->parse($my_content);


thanks. i feel stupid now blush.gif
Go to the top of the page
 
+Quote Post
Trap FeedBacker
post May 1 2008, 01:57 PM
Post #8


Guest Feedbacks
***************

Group: Members
Posts: 2,360
Joined: 21-September 07
Member No.: 50,369



Need a BB on my website http://www.notrac.net
Php Bbcode

Replying to Blessed

Need help to write a html code / bbcode that I can put on my website.

The members would like to write news on their profilepage without writing webmaster a mail. It don't have to be very pro but keep it simple.

Help? Please write to me by mail.

With kind regards

Vagabondo

-reply by Terje Jensen
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics