Advanced Search

Results 1 to 8 of 8

Thread: Custom BB code function in PHP

  1. #1
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    261
    Thanked 690 Times in 678 Posts

    Default Custom BB code function in PHP

    I wrote this code to parse BB code for a website I made.

    It includes [b],[u],[i],[link=], and [spoiler]*
    (*this is for a site talking about movies, so a spoiler is something that would give away part of the plot, so it hides that from being displayed. Easy enough to remove this from the code, though)

    It also parses links, like http://, https://, mailto:, and ftp://.
    (It doesn't yet parse emails by themselves.)

    Emoticons are also easy.
    For now I'll just include examples of and .

    PHP Code:
    function badlink($link$prefix) {
        if (
    $prefix == "mailto:") {
            if (
    strpos($link"@") === FALSE || strpos($link".", (strpos($link"@")+2)) === FALSE || substr_count($link"@") > || strpos($link"@") == 0) {
                return 
    1;
                }
            }
        if (
    strpos($link".") == || strpos($link".") == strlen($link) || (strpos($link"/") < strpos($link".") && strpos($link"/") !== FALSE)) {
            return 
    1
            }
        };
    function 
    setlinks($r$prefix) {
        if (
    substr($r0strlen($prefix)) == $prefix) {
            
    $r "\n".$r;
            }
        
    $r str_replace("<br>".$prefix"<br>\n".$prefix$r);
        
    $r str_replace(" ".$prefix" \n".$prefix$r);
        while (
    strpos($r"\n".$prefix) !== FALSE) {
            list(
    $r1$r2) = explode("\n".$prefix$r2);
            if (
    strpos($r2" ") === FALSE && strpos($r2"<br>") === FALSE) {
                if (
    $prefix != "mailto:") {
                    
    $target ' target="_blank"';
                    }
                else {
                    
    $target "";
                    }
                if (
    strpos($r2".") > && strpos($r2".") < strlen($r2) && badlink($r2$prefix) != 1) {
                    
    $r $r1.'<a href="'.$prefix.$r2.'"'.$target.'>'.$prefix.$r2.'</a>';
                    }
                else {
                    
    $r $r1.$prefix.$r2;
                    }
                }
            else {
                if (
    strpos($r2" ") === FALSE || ( strpos($r2" ") > strpos($r2"<br>") && strpos($r2"<br>") !== FALSE)) {
                    list(
    $r2$r3) = explode("<br>"$r22);
                    if (
    badlink($r2$prefix) != 1) {
                        
    $r $r1.'<a href="'.$prefix.$r2.'"'.$target.'>'.$prefix.$r2.'</a><br>'.$r3;
                        }
                    else {
                        
    $r $r1.$prefix.$r2.'<br>'.$r3;
                        }
                    }
                else {
                    list(
    $r2$r3) = explode(" "$r22);
                    if (
    strpos($r2".") > && strpos($r2".") < strlen($r2) && badlink($r2$prefix) != 1) {
                        
    $r $r1.'<a href="'.$prefix.$r2.'"'.$target.'>'.$prefix.$r2.'</a> '.$r3;
                        }
                    else {
                        
    $r $r1.$prefix.$r2.' '.$r3;
                        }
                    }
                }
            }
        return 
    $r;
        };
    function 
    bb($r) {
        
    $r trim($r);
        
    $r htmlentities($r);
        
    $r str_replace("\r\n","<br>",$r);
        
    $r str_replace("[b]","<b>",$r);
        
    $r str_replace("[/b]","</b>",$r);
        
    $r str_replace("[i]","<i>",$r);
        
    $r str_replace("[/i]","</i>",$r);
        
    $r str_replace("[u]","<u>",$r);
        
    $r str_replace("[/u]","</u>",$r);
        
    $r str_replace("[spoiler]",'[spoiler]<font color="#DDDDDD">',$r);
        
    $r str_replace("[/spoiler]","</font>[/spoiler]",$r);
        
        
    //set [link]s
        
    while (strpos($r"[link=") !== FALSE) {
            list (
    $r1$r2) = explode("[link="$r2);
            if (
    strpos($r2"]") !== FALSE) {
                list (
    $r2$r3) = explode("]"$r22);
                if (
    strpos($r3"[/link]") !== FALSE) {
                    list(
    $r3$r4) = explode("[/link]"$r32);
                    
    $target ' target="_blank"';
                    if (
    substr($r207) == "mailto:") {
                        
    $target "";
                        }
                    
    $r $r1.'<a href="'.$r2.'"'.$target.'>'.$r3.'</a>'.$r4;
                    }
                else {
                    
    $r $r1."[link\n=".$r2."]".$r3;
                    }
                }
            else {
                
    $r $r1."[link\n=".$r2;
                }
            }
        
    $r str_replace("[link\n=","[link=",$r);
        
    ////[link]
        
        ///default url link setting
        
    $r setlinks($r"http://");
        
    $r setlinks($r"https://");
        
    $r setlinks($r"ftp://");
        
    $r setlinks($r"mailto:");
        
    ////links
        
        ///emoticons
        
    $r str_replace(":)",'<img src="myhappyimg.gif">',$r);
        
    $r str_replace(":(",'<img src="mysadimg.gif">',$r);
        
    //Repeat for more if desired.
        ///emoticons
        
        
    $r trim($r);
        return 
    $r;
        } 
    Notes: The first function is used in the second and the second in the third, main function. So you don't want to change the order of these. Also, you cannot set a function within another because if it's called twice, an error will be generated.

    To use... VERY simple:
    PHP Code:
    $text bb($text); 

    I don't claim this is perfect, but I think it works well enough. It doesn't check for tags left open, but that's just something that your users will need the common sense to deal with.
    Note that using a <div> or a similar element around each instance of this when placed back into your page might be a good idea so that you don't have miscellaneous stray <b>s, etc., leaving your page open to be made bold throughout.


    Also, this function is html injection SAFE. The BB codes generate html, but any html entered will be converted to html entities and rendered harmless.

    And here's a page listing the functions:
    http://thebrb.com/theater/index.php?...ps&type=review
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Thanks!

    No image tag?

    Edit: Don't worry, will do it myself
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    261
    Thanked 690 Times in 678 Posts

    Default

    Note: [img=]...[/img] is identical in coding to the [link] tag. Just do the same
    Also, you might want to verify that it is an image with imagesize() to see if it parses as such.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Nice script djr (I know you already showed me, but... anyways). I'm going to be implementing it soon.
    - Mike

  5. #5
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Not bad, but you are very limited if you don't use regular expressions. They can be confusing, but are well with. As an example, I put together how to do this with regular expressions. It supports b,i,u,quote,s,list(and * ),url(both kinds),img,color,size,smilies,auto link urls,auto link email addresses, and converts newlines to <br> tags.
    Test it: http://www.webtech101.com/uploads/dd/bb_code/index.php
    See the code: http://www.webtech101.com/uploads/dd...e_bb_code.phps

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    261
    Thanked 690 Times in 678 Posts

    Default

    Interesting. I never claimed regex are bad... just never dealt with learning them.
    I'll take a look when I get the chance. (At the code, I mean... saw the demo.)

    I think that el/strong should not be used... b=b, i=i... no?
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Hmm...Maybe, but strong and em are always better than b/i which are presentational markup.

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    261
    Thanked 690 Times in 678 Posts

    Default

    Not always. Or they wouldn't exist.

    <b> and <i> are presentational... yes. If your design called for a specific type of look for the em or strong tags, then your user would be confused.
    BB users would always expect bold from b and italics from i. In that case, it SHOULD dictacte the graphic look of the text.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •