SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Enthusiast bobbobbobbob's Avatar
    Join Date
    Apr 2004
    Location
    bobbobbobbob
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best way to allow bbcode?

    How would I go about putting bbcode on my site?

    does it have something to do with str_replace?

    PHP Code:
    if(preg_match("[b]",$m)){
    echo 
    str_replace("[b]","<b>",$m);
    }
    if(
    preg_match("[/b]",$m)){
    echo 
    str_replace("[/b]","</b>",$m);

    I tried this, but it shows nothing.....help?

  2. #2
    Non-Member St.'s Avatar
    Join Date
    Nov 2004
    Location
    Cloud #9
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and you can search on this forum. I saw many topics with bbcode transformators

  3. #3
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't see any reason to do a preg_match. Just Put your simple replaces into an array. such as.

    PHP Code:
       
    $bbcode 
    = array(
                     
    '[ b ]' => '<strong>',  //spaces added cuz of SP parcer.
                     
    '[ /b ]' => '</strong>',
                     
    'etc....'
                     
    ); 
    Then just run the str_replace.
    PHP Code:
       $text str_replace(array_keys($bbcode), array_values($bbcode), $text); 
    If it doesn't find it, it just won't replace anything. No need for the overhead of checking for each one. For mor complex bbcode you will need to do the same but useing preg_replace.

  4. #4
    Non-Member St.'s Avatar
    Join Date
    Nov 2004
    Location
    Cloud #9
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my version 8)

    PHP Code:
    /**
     * class BBCodeReconstructor
     *
     * class implements mechanism for reconstruct bbcode to
     * html code and back
     *
     * @author Nemshilov N.V. aka St. <wingley@mail.ru>
     * @package lib
     * @version 0.4
     */
    class BBCodeReconstructor{
        
    /**
         * constructor
         *
         * @return void
         * @access public
         */
        
    function BBCodeReconstructor(){
        }
        
        
    /**
         * method for add new bbkey for reconstruction
         *
         * @param reference to array
         * @return void
         * @access public
         */
        
    function addKeys( &$arr ){
            if ( 
    is_array$arr ) )
                
    $this->keys array_merge$this->keys$arr );
            else
                die( 
    "Tags collection must be array" );
        }
        
        
    /**
         * method set possibles keys
         *
         * @param array
         * @return void
         * @access public
         */
        
    function setPossibleTags$arr ){
            if ( 
    is_array$arr ) )
                
    $this->possibleTags $arr;
            else
                die( 
    "Tags collection must be array" );
        }
        
        
    /**
         * method make bbcode to html reconstructrion
         *
         * @param string BB code
         * @return string html code
         * @access public
         */
        
    function getReconstructed$bb_code ){
            foreach( 
    $this->possibleTags as $key ){
                if ( isset( 
    $this->keys[$key] ) && is_array$this->keys[$key] ) ){
                    foreach( 
    $this->keys[$key] as $bb => $html ){
                        
    $bb_code str_replace$bb$html$bb_code );
                    }
                }
            }
            return 
    $bb_code;
        }
        
        
    /**
         * method make back reconstruction from html to bbcode
         *
         * @param string html code
         * @return string bb code
         * @access public
         */
        
    function getBackConstucted$html_code ){
            foreach( 
    $this->possibleTags as $key ){
                if ( isset( 
    $this->keys[$key] ) && is_array$this->keys[$key] ) ){
                    foreach( 
    $this->keys[$key] as $bb => $html ){// echo $html." => ".$bb."<BR>";
                        
    $html_code str_replace$html$bb$html_code );
                    }
                }
            }
            return 
    $html_code;
        }
        
        var 
    $possibleTags = array();
        var 
    $keys = array(
                
    "b"     => array(   "[b]"       => "<b>",
                                    
    "[/b]"      => "</b>"   ),
                
    "u"     => array(   "[u]"       => "<u>",
                                    
    "[/u]"      => "</u>"   ),
                
    "i"     => array(   "[i]"       => "<i>",
                                    
    "[/i]"      => "</i>"   ),
                
    "h4"    => array(   "[h4]"      => "<h4>",
                                    
    "[/h4]"     => "</h4>"  ),
                
    "h3"    => array(   "[h3]"      => "<h3>",
                                    
    "[/h3]"     => "</h3>"  ),
                
    "h2"    => array(   "[h2]"      => "<h2>",
                                    
    "[/h2]"     => "</h2>"  ),
                
    "h1"    => array(   "[h1]"      => "<h1>",
                                    
    "[/h1]"     => "</h1>"  ),
                
    "img"   => array(   "[img]"     => "<img src='",
                                    
    "[/img]"    => "' border=0>" )
        );


  5. #5
    Non-Member St.'s Avatar
    Join Date
    Nov 2004
    Location
    Cloud #9
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    using

    PHP Code:
    $bb_string "bla bla bla ";

    $btr =& new BBCodeReconstructor();
    $btr->setPossibleTags( array( "b""u""i""img" ) );
    $html_string $btr->getReconstructed$bb_string );

    // back bb code construction
    $bb_string $btr->getConstructed$html_string ); 
    and you can add your extra bbtags for reconstructrion
    PHP Code:
    $extra_tags = array(
       
    "bla"  => array("[bla/]" => "<bla />"),
       
    "nunga" => array( "[nunga]" => "<nunga>",
                                    
    "[/nunga]" => "</nunga>" )
    );

    $btr->addKeys$extra_tags ); 

  6. #6
    SitePoint Enthusiast bobbobbobbob's Avatar
    Join Date
    Apr 2004
    Location
    bobbobbobbob
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all for that.

    But one more question....

    How would I allow links in bbcode?

    I did some experimenting and I cant figure it out.

  7. #7
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to use regular expresions and preg_replace for the complex ones. Here is a snippet from one of mine. I'd give you more but I just got back from Sinus surgery and I don't think I'll be doin much tonight.

    PHP Code:
     $preg = array(
     
    // Raw links
     
    '/([\s])([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i'  => '$1<a href="$2">[Link]</a>',
     
    // [url]
     
    '/\[url\]www\.(.*?)\[\/url\]/si'                 => '<a href="http://www.\\1" target="_blank">\\1</a>',
     
    '/\[url=(.*?)\](.*?)\[\/url\]/si'                => '<a href="\\1" target="_blank">\\2</a>'
     
    );
     
     
    $text preg_replace(array_keys($preg), array_values($preg), $text);
     
    ?> 

  8. #8
    Non-Member St.'s Avatar
    Join Date
    Nov 2004
    Location
    Cloud #9
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in nonstandard you can do it without regexps

    for example
    [link][ur l]http://www.host.bla/bla/bla[/u rl] bla bla bla [/link]

    PHP Code:
    $tags = array(
        
    "[link]"    => "<a",
        
    "[url]"     => " href='",
        
    "[/url]"    => "' target='blank'",
        
    "[/link]"   => "</a>"
    ); 

  9. #9
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's non standard bbcode. I wouldn't do that.

  10. #10
    Non-Member St.'s Avatar
    Join Date
    Nov 2004
    Location
    Cloud #9
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it was just for example.
    by the way, where i can see all standards bbtags?

  11. #11
    Non-Member St.'s Avatar
    Join Date
    Nov 2004
    Location
    Cloud #9
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.sitepoint.com/forums/misc.php?do=bbcode

    this is all bbtags? tags of smiles are bbtags standard or not?

  12. #12
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Writing a proper parser for any language is not too difficult but requires some know-how.

    Basically you need a piece of code that will scan through the string and halt when it encounters the start or end of a bbcode tag, or a word, or a symbol. Then you need another piece of code which can decide what to do with the tag and keeps track of which tags are currently open, etc. It is a bit like a tokeniser and parser, respectively. Think of the string linearly.

    Ok lets say that the last character was plain text and the next character is an open square bracket. That means you've encountered the start of bbcode. Check if the next character is a slash, which would indicate a closing tag. It is. Check the next word to find what type of tag it is. The next word is 'url'. Check which tags are currently open. There is a url tag open. So, we close that. Output '</a>'.

    And so on and so forth.

    A standard search and replace job or regular expression will have problems. For instance, what if you have an end tag but no beginning tag?

    Code:
    This is not a link.[/url]
    Using a plain regular expression or search and replace would output:

    Code:
    This is not a link.</a>
    You need to have logic which recognises which tags are currently open at that point if the code and checks to ensure that a hyperlink is indeed open.
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  13. #13
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Fortunately, there exist some parsers for parsing BBcode and outputting HTML already which you can use. There is one in PEAR for example. They have their respective problems, for example their output is standards-compliant to varying degrees. It is worth checking some out, because if you do find one that's right for you, then at least you didn't have to invent the wheel.

    If you're developing an application that cannot be licensed as open source you may have to develop your own, of course.
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  14. #14
    Non-Member St.'s Avatar
    Join Date
    Nov 2004
    Location
    Cloud #9
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    оh! i have translucence! 8)
    uncompleted tags - big hole.

  15. #15
    SitePoint Zealot Caged's Avatar
    Join Date
    May 2003
    Location
    United States
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've written a simple function you might find useful.
    http://www.isolated-designs.net/core...code-functions

  16. #16
    Non-Member St.'s Avatar
    Join Date
    Nov 2004
    Location
    Cloud #9
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    2Caged:

    are you can make back reconstuction regexps?
    i mean for html to bbcode reconstruction

  17. #17
    SitePoint Enthusiast bobbobbobbob's Avatar
    Join Date
    Apr 2004
    Location
    bobbobbobbob
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for bothering you all again with another question.....but I only have one more, then I will stop bothering you....heh

    I started to use reg. expressions and ran into another road block.

    PHP Code:
    $bb = array(
    '[url=(http|https|ftp)://)([[:alnum:]-])+(\.)([[:alnum:]-]){2,4}([[:alnum:]/+=%&_.~?-]*)]([[:alpha:]. -]{1,})' => '<A HREF=\"1\">1',
        ); 
    First of all, the regular expression isnt working and I cant figure out why....and the second thing is, what do I put in the <A HREF="?">?? part?

    How can I make the ?? fill in with the appropriate values from before?

    Thanks.

  18. #18
    SitePoint Zealot
    Join Date
    Dec 2002
    Location
    Asker, Norway
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use the BBCode class at phpclasses.org.
    It works great
    http://www.phpclasses.org/browse/file/3199.html

  19. #19
    Obsessive designer Infizi's Avatar
    Join Date
    May 2004
    Location
    North Pole
    Posts
    450
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a way to get round this is in a loop, you need to urn a tag open and tag close, so if the variable = tag closed then you remove the tag. of yourse this would need to use a 3d array, because you bould have b i /i /b

    Quote Originally Posted by mmj
    Writing a proper parser for any language is not too difficult but requires some know-how.

    Basically you need a piece of code that will scan through the string and halt when it encounters the start or end of a bbcode tag, or a word, or a symbol. Then you need another piece of code which can decide what to do with the tag and keeps track of which tags are currently open, etc. It is a bit like a tokeniser and parser, respectively. Think of the string linearly.

    Ok lets say that the last character was plain text and the next character is an open square bracket. That means you've encountered the start of bbcode. Check if the next character is a slash, which would indicate a closing tag. It is. Check the next word to find what type of tag it is. The next word is 'url'. Check which tags are currently open. There is a url tag open. So, we close that. Output '</a>'.

    And so on and so forth.

    A standard search and replace job or regular expression will have problems. For instance, what if you have an end tag but no beginning tag?

    Code:
    This is not a link.[/url]
    Using a plain regular expression or search and replace would output:

    Code:
    This is not a link.</a>
    You need to have logic which recognises which tags are currently open at that point if the code and checks to ensure that a hyperlink is indeed open.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •