GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Oct-2005, 15:50
Kilgayne's Avatar
Kilgayne Kilgayne is offline
Junior Member
 
Join Date: Jun 2004
Location: somewhere
Posts: 52
Kilgayne is on a distinguished road

BBcode clone


hi, I've been trying to make a clone of the BB Code for my forum (I always try to make my own stuff you see) and... well this is the function I made :

PHP Code:

function code_format($tagged)
{
  // -- FONT Tag --
  $tagged=eregi_replace('(\[font=([[:alnum:][:space:]]*)\])','<font face="\\2">',$tagged);

  // -- PARA Tag --
  $tagged=eregi_replace('(\[para=([[:alpha:]]*)\])','<p align="\\2">',$tagged);
  $tagged=eregi_replace('(\[para\])','<p>',$tagged);

  // -- B, I, S and U Tags --
  $tagged=eregi_replace('\[b\]','<b>',$tagged);
  $tagged=eregi_replace('\[i\]','<i>',$tagged);
  $tagged=eregi_replace('\[s\]','<s>',$tagged);
  $tagged=eregi_replace('\[u\]','<u>',$tagged);
  $tagged=eregi_replace('\[/b\]','</b>',$tagged);
  $tagged=eregi_replace('\[/i\]','</i>',$tagged);
  $tagged=eregi_replace('\[/s\]','</s>',$tagged);
  $tagged=eregi_replace('\[/u\]','</u>',$tagged);

  // -- SIZE Tag --
  $tagged=eregi_replace('(\[size=([[:digit:]]*)\])','<font size="\\2">',$tagged);

  // -- COLOR Tag --
  $tagged=eregi_replace('(\[color=([[:xdigit:]]*)\])','<font color="#\\2">',$tagged);

  // -- URL Tag --
  $tagged=eregi_replace('(\[url=([[:alnum:]_:/.@]*)\])','<a href="\\2">',$tagged);
  $tagged=eregi_replace('(\[url\])([[:alnum:]_:/.@]*)(\[/url\])','<a href="\\2">\\2</a>',$tagged);

  // -- EMAIL Tag --
  $tagged=eregi_replace('(\[email=([[:alnum:]_:/.@]*)\])','<a href="mailto:\\2">',$tagged);
  $tagged=eregi_replace('(\[email\])([[:alnum:]:_/.@]*)(\[/email\])','<a href="mailto:\\2">\\2</a>',$tagged);

  // -- IMG Tag --
  $tagged=eregi_replace('(\[img=([[:alnum:]_:/.@]*)\])','<img src="\\2">',$tagged);
  $tagged=eregi_replace('(\[img\])([[:alnum:]_:/.@]*)(\[/img\])','<img src="\\2">',$tagged);

  // -- LIST, NLIST and ALIST Tag --
  $tagged=eregi_replace('\[list\]','<ul>',$tagged);
  $tagged=eregi_replace('\[/list\]','</ul>',$tagged);
  $tagged=eregi_replace('\[nlist\]','<ol>',$tagged);
  $tagged=eregi_replace('\[/nlist\]','</ol>',$tagged);
  $tagged=eregi_replace('\[alist\]','<ol type="a">',$tagged);
  $tagged=eregi_replace('\[/alist\]','</ol>',$tagged);
  $tagged=eregi_replace('\[\*\]','<li>',$tagged);

  // -- Tag Closing --
  $tagged=eregi_replace('\[/size\]','</font>',$tagged);
  $tagged=eregi_replace('\[/color\]','</font>',$tagged);
  $tagged=eregi_replace('\[/font\]','</font>',$tagged);
  $tagged=eregi_replace('\[/url\]','</a>',$tagged);
  $tagged=eregi_replace('\[/email\]','</a>',$tagged);

  return $tagged;
} 



now I want the program to check if a tag has been closed and if not, it closes it itself. I thought I could substract the numbers of open tags to the number of closed tags but the function "eregi" (or "ereg", though it's case sensitive) just tells if there's an occurence and not how many

please help me!! i wanted to create the "quote", "code" and "highlight" tags but if the user doesnt close these it could ruin the appearance of my site \o/
  #2  
Old 16-Oct-2005, 00:55
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: BBcode clone


The bbcode class I wrote for www.gidnetwork.com is quite efficient in that it only goes through a string once.

I am not a c or c++ programmer, but I assume each call to eregi_replace() or preg_replace() triggers a scan of the entire string, which I think is completely unnecessary. This is why I wrote my own version which does nearly everything in just one pass.

I don't want to paste the entire class here but I will go through the script today, extract the relevant bits and post a much "lighter" version of it as soon as possible. If it does nothing else, it will encourage you to write yours again and to do it quite differently.

... and yes, it handles (auto-closes) opened bbcode tags at the end of the string.
  #3  
Old 16-Oct-2005, 06:38
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: BBcode clone


As promised, here is the basic (lite) version of my bbcode class:

PHP Code:

<?php
/**
 * Class: GIDBBcode parser (lite).
 * 
 * Class; GIDBBcode parser (lite).  Not production quality.  Just an example, proof of concept.
 *
 * @author       J de Silva                             <giddomains@gmail.com>
 * @copyright    Copyright &copy; 2005, J de Silva
 * @link         http://www.gidforums.com/t-7428.html   BBcode clone
 */
class GIDBBCode
{
    function GIDBBCode(){}
    
    function parse( $str )
    {// considering that $str was already passed through htmlspecialchars();
        $this->_bbcodetag    = array('opened'=>array());
        $length         = strlen( $str );
        $position         = 0;
        $this->_intag        = false;
        $this->_new        = null;
        
        while( $position<$length )
        {
            $c = $str{$position};
            if( $this->_intag )
                $this->_process_intag( $c );
            elseif( $c=='[' )
                $this->_intag = $c;
            else
                $this->_new .= $c;
            ++$position;
        }
        
        // handle incomplete intags at the end of the string.
        if( $this->_intag )
            $this->_new .= $this->_intag;
        // auto-close any missing (closing) bbcode tags! e.g. "This is in [b]Bold."
        if( $this->_bbcodetag['opened'] )
        {
            $close = array_reverse( $this->_bbcodetag['opened'] );
            foreach( $close as $code )
                $this->_markup_close_tag( $code );
        }        
        unset( $str );
        return( $this->_new );
    }
    
    // PRIVATE METHODS
    /////////////////////////////////////////////////////////////
    function _markup_close_tag( $code )
    {
        if( in_array($code, $this->_bbcodetag['opened']) )
        { // only process if a matching OPENED tag was found. e.g. the last [/b] in "[b]Bold[/b][/b]" will not be processed.
            switch( $code )
            {
                case    'b':        $markup .= '</strong>';        break;
                case    'code':        $markup .= '</pre></code>';    break;
                case    'font':        $markup .= '</span>';        break;
                case    'i':        $markup .= '</em>';            break;
            }
            array_pop( $this->_bbcodetag['opened'] );
        }
        else
            $markup = "[/$code]";
        $this->_new .= $markup;
    }
    
    function _markup_open_tag( $code )
    {
        $option = null;
        if( false!==strpos($code, '=') )
        {// extract "font" and "arial" from bbcode tags with options; like "font=arial" for example.
            $tmp = explode( '=', $code, 2 );
            $code = &$tmp[0];
            $option = &$tmp[1];
        }
        $valid_tag = true;
        switch( $code )
        {
            case    'b':        $markup .= '<strong>';            break;
            case    'code':        $markup .= '<code><pre>';        break;
            case    'font':        $markup .= '<span style="font-family:' .
                                            $this->_verifyFont( $option ) .
                                            ';">';
                                            break;
            case    'i':        $markup .= '<em>';                break;
            default:
                if( $option )
                    $option = '=' . $option;
                $markup = "[$code$option]";
                $valid_tag = false;
        }
        if( $valid_tag )
            $this->_bbcodetag['opened'][] = $code;
        $this->_new .= $markup;
    }
    
    function _process_bbcode_tag()
    {
        $tag = substr( $this->_intag, 1 );
        if( $tag{0}==='/' )
            $this->_markup_close_tag( substr($tag, 1) );
        else
            $this->_markup_open_tag( $tag );
        $this->_intag = false;
    }
    
    function _process_intag( $c )
    {
        if( $c===']' )
            $this->_process_bbcode_tag();
        elseif( $c==='[' ) // i.e. the previous "[" character was bogus! e.g. [[b]Something]
        {
            $this->_new .= $this->_intag;
            $this->_intag = $c;
        }
        else
            $this->_intag .= $c;
    }
    
    function _verifyFont( $option )
    {
        switch( $option )
        {
            // allow only what we allow as options!
            case    'arial':
            case    'courier':
            case    'courier new':
            case    'times new roman':
            case    'verdana':
                if( false!==strpos($option, ' ') )
                    $option = "'$option'"; // e.g. {courier new} is reset to {'courier new'} i.e. with apostrophes
                $font = $option;
                break;            
            default:
                $font = 'verdana';
        }
        return( $font );
    }
} 
?>


If you look closely at the method: parse( $str ), you'll see that it translates all the bbcodes (in a string) in a single pass.

If you saved this example code to a file, say, GIDBBCode.inc.php, you would use it like this on a regular script/web page:

PHP Code:

<?php

// filename: messages.php

//include the bbcode class
include_once( './GIDBBCode.inc.php' );

// a dummy string
$sample = 'Something in [b]bold and [i]italics[/i][/b].  Let\'s leave out a couple of closing [b]bbcode tags [i]here.';
$sample = htmlspecialchars( $sample );

$BBCODE = &new GIDBBCode();
$sample = $BBCODE->parse( $sample );
echo "<p>$sample</p>\n";
?>


Again, this is an extract from my original bbcode handler class, so I may have introduced some bugs in the process.
  #4  
Old 27-Oct-2005, 10:23
djd@n djd@n is offline
New Member
 
Join Date: Oct 2005
Posts: 21
djd@n is on a distinguished road

Re: BBcode clone


JDS you are soo clever
  #5  
Old 11-Jan-2006, 22:46
EliteShadow EliteShadow is offline
New Member
 
Join Date: Jan 2006
Posts: 3
EliteShadow is on a distinguished road

Re: BBcode clone


JDS:

In your regular version, did you by any chance create a PHP parser?
  #6  
Old 15-Jan-2006, 06:51
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: BBcode clone


If you mean "php syntax highlighter" by "PHP parser", then yes, I did. See the code example here. Why?

Besides PHP, we also have custom syntax highlighters for other popular languages like C++, C#, Java, python, VB and even CSS markup.
  #7  
Old 15-Jan-2006, 16:57
EliteShadow EliteShadow is offline
New Member
 
Join Date: Jan 2006
Posts: 3
EliteShadow is on a distinguished road

Re: BBcode clone


That link that you gave me does not have any example for parsing php.

Well, I am writing my own forum script, and I would like a PHP parser, but I cannot find ANYTHING that works. I think that for some reason, the PHP tags go through the server and do not show anything BEFORE I can even parse them.
  #8  
Old 15-Jan-2006, 17:21
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: BBcode clone


Quote:
Originally Posted by EliteShadow
That link that you gave me does not have any example for parsing php.

Well, I am writing my own forum script, and I would like a PHP parser, but I cannot find ANYTHING that works.

You could take a look at this script. It is released under the GPL for your parsing pleasure.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #9  
Old 15-Jan-2006, 18:13
EliteShadow EliteShadow is offline
New Member
 
Join Date: Jan 2006
Posts: 3
EliteShadow is on a distinguished road

Re: BBcode clone


I was looking for a [php] tag parser, but this does help with other tags! Thanks a bunch...
  #10  
Old 23-Feb-2006, 23:14
Kilgayne's Avatar
Kilgayne Kilgayne is offline
Junior Member
 
Join Date: Jun 2004
Location: somewhere
Posts: 52
Kilgayne is on a distinguished road

Re: BBcode clone


thanks for the code JayDeeEss, and sorry for not responding earlier! ^^;

and btw, there are indeed some bugs in your class >_o just one, to be precise, you didn't initialize the $markup variable. But the rest is pure gold, thanks a lot!!
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: BBCode - the [quote] tag JdS PHP Code Library 0 05-May-2005 06:05
[Discussion] New HTML BBCode. jrobbio Web Design Forum 8 14-Oct-2004 14:20
Php bbcode issue Caged MySQL / PHP Forum 3 06-Aug-2003 18:55
GIDForums enables New [CSS] bbcode. admin GIDForums™ 0 01-Jun-2003 23:57

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:31.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.