BBCode Help

coolhomecoolhome Junior MemberShared Hoster
Well ive been looking into making a bbcodes in my forums that is almost done. Im adding extra things like smiles + bbcodes and ect. Well i dont know preg so can someone make me a script.

so far all i got (#\[b\](.*?)\[/b\]#si), (#\[u\](.*?)\[/u\]#si), (#\[i\](.*?)\[/i\]#si).

I need some preg made for me lool

,[php][/php] (highlight),Name / link, link, link,
,[email=link]Name[/email] OR [email]link[/email]

lol im new to preg and i am learning (slowly :()

Thanks

Comments

  • NuvoNuvo Forum Leader VPS - Virtual Prince of the Server
    I don't know how much use this will be as it's pretty old now, but here:
    [php]
    /* BBCode parsing (extended BBCode parser). */


    /* Turn B into strong (bolded) tags... */
    $input = preg_replace("/\[b\](.+?)\[\/b\]/is","<strong>$1</strong>",$input);


    /* Turn I into itallic (emphesis) tags (should really use <em></em>) */
    $input = preg_replace("/\[i\](.+?)\[\/i\]/is","<i>$1</i>",$input);


    /* Turn U into underscored text (CSS may be a better alternative here) */
    $input = preg_replace("/\[u\](.+?)\[\/u\]/is","<u>$1</u>",$input);


    /* Change S into struck out text */
    $input = preg_replace("/\[s\](.+?)\[\/s\]/is","<s>$1</s>",$input);


    /* Change alignment for a text block */
    $input = preg_replace("/\[align\=(.+?)\](.+?)\[\/align\]/is","<div style=\"text-align: $1;\">$2</div>",$input);


    /* Set the font for a text block using spans to combat depreciated taging */
    $input = preg_replace("/\[font\=(.+?)\](.+?)\[\/font\]/is","<span style=\"font-family: $1;\">$2</span>",$input);


    /* Set the size of a text block using spans */
    $input = preg_replace("/\[size\=(.+?)\](.+?)\[\/size\]/is","<span style=\"font-size: $1;\">$2</span>",$input);


    /* Set the font colour of a text block using spans */
    $input = preg_replace("/\[color\=(.+?)\](.+?)\[\/color\]/is","<span style=\"color: $1;\">$2</span>",$input);


    /* Put an image in the post */
    $input = preg_replace("/\[img\](.+?)\[\/img\]/is","<img src=\"$1\" alt=\"user posted image\"/>",$input);

    /* Put an image in the post with a custom title (modify this to make image links) */

    $input = preg_replace("/\[img\=(.+?)\](.+?)\[\/img\]/is","<img src=\"$2\" alt=\"$1\" title=\"$1\"/>",$input);


    /* Put a link in the post */
    $input = preg_replace("/\[url\](.+?)\[\/url\]/is","<a href=\"$1\">$1</a>",$input);

    /* Put a link in the post with a custom title */

    $input = preg_replace("/\[url\=(.+?)\](.+?)\[\/url\]/is","<a href=\"$1\">$2</a>",$input);


    /* Put a code block into the post */
    $input = preg_replace("/\[code\](.+?)\[\/code\]/is","<div style=\"background: #ffffff; border: 1px solid #333333; padding: 1px;\">

    <code>$1</code></div>",$input);

    /* end of BBCode parsing. */
    [/php]

    Slightly newer and more elegant version:
    [php]
    //Main tags:

    $p[] = "/\[b\](.+?)\[\/b\]/is";

    $r[] = "<strong>$1</strong>";

    $p[] = "/\[i\](.+?)\[\/i\]/is";

    $r[] = "<i>$1</i>";

    $p[] = "/\[u\](.+?)\[\/u\]/is";

    $r[] = "<u>$1</u>";

    $p[] = "/\[u\](.+?)\[\/u\]/is";

    $r[] = "<u>$1</u>";

    $p[] = "/\[s\](.+?)\[\/s\]/is";

    $r[] = "<s>$1</s>";

    $p[] = "/\[align\=(.+?)\](.+?)\[\/align\]/is";

    $r[] = "<div style=\"text-align: $1;\">$2</div>";

    $p[] = "/\[font\=(.+?)\](.+?)\[\/font\]/is";

    $r[] = "<span style=\"font-family: $1;\">$2</span>";

    $p[] = "/\[size\=(.+?)\](.+?)\[\/size\]/is";

    $r[] = "<span style=\"font-size: $1;\">$2</span>";

    $p[] = "/\[color\=(.+?)\](.+?)\[\/color\]/is";

    $r[] = "<span style=\"color: $1;\">$2</span>";

    $p[] = "/\[img\](.+?)\[\/img\]/is";

    $r[] = "<img src=\"$1\" alt=\"user posted image\"/>";

    $p[] = "/\[img\=(.+?)\](.+?)\[\/img\]/is";

    $r[] = "<img src=\"$2\" alt=\"$1\" title=\"$1\"/>";

    $p[] = "/\[url\](.+?)\[\/url\]/is";

    $r[] = "<a href=\"$1\">$1</a>";

    $p[] = "/\[url\=(.+?)\](.+?)\[\/url\]/is";

    $r[] = "<a href=\"$1\">$2</a>";

    $p[] = "/\[code\](.+?)\[\/code\]/is";

    $r[] = "<div style=\"background: #ffffff; border: 1px solid #333333; padding: 1px;\">

    <code>$1</code></div>";

    $input = preg_replace($p,$r,$input); //This is where the replacement begines. It'll cycle through them all and leave you with HTML code... P = pattern, R = replacement, so the pattern for bold tags is replaced with the replacement for bold tags.

    [/php]
    PHP, CSS, XHTML, Delphi, Ruby on Rails & more.
    Current project: CMS Object.
    Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in.
    Release date: NEVER!!!
  • coolhomecoolhome Junior Member Shared Hoster
    Thank you alot... lol u went a lil over than i wanted but hey its better i guess lol. Ill put you in credits under Special Help...
  • WatchOutWatchOut Advanced User VPS - Virtual Prince of the Server
    Yeah, remember you can also add a green reputation for Nuvo, since he spent a lot of time helping you.
    Computer Forumz

    www.computerforumz.com
  • dsilwaldsilwal Beginner Link Clerk
    hey,
    how can i intergrate that code into my download cms?
    <?php
    include("../dbconnect.php");
      if(isset($_POST['submit']))
      {
    
    $date = date("d/m/Y");
    $title = addslashes($_POST['title']);
    $description = addslashes($_POST['description']);
    $link = addslashes($_POST['link']);
    $query = "INSERT INTO `downloads` (title, date, description, link,) VALUES ('$title','$date','$description', '$link')";
    $result = mysql_query($query);
    
    if ($result) {
    echo "<b>Download Added</b><br/>You can add another in 2 seconds.";
    echo "<meta http-equiv=Refresh content=2;url=submit.php>";
    } else {
    echo "Download could not be added.<br/>";
    }
                  }
    else
      {
          ?>
                        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                          <div align="left"><span class="style10">Title:</span> <br>
                            <input name="title" size="70" maxlength="255">
                            <br>
                            <br>
                            <span class="style10"> Description:</span><br>
                              <textarea name="description"  rows="12" cols="120"><img src=>
    <br></textarea>
                            <br>
                            <br>
                            <span class="style10"> Link(s):</span><br> 
                                <textarea name="link"  rows="12" cols="120"><br>
    </textarea>
                            <br>
                            <br>
                            <span class="style8">Password:</span><br>
                            <input name="pass" size="70" maxlength="255">
                            <br>
                            <br>
                            <input type="submit" name="submit" value="Add Download">
                          </div>
                </form>
                        
                        <div align="left">
      <?php
      }
    
    ?>
    
  • NuvoNuvo Forum Leader VPS - Virtual Prince of the Server
    Well, you could either parse when the page leads (post parsing) or before you insert data into the database (pre parsing).
    In terms of ease of use, post parsing is the simplest as you only need to store the data once with no reverse parsing (HTML to BBCode), but usually, you'll want to go with pre parsing because it gets around any front-end lag caused by the parser (all the BBCode script is doing is parsing out BBCode as standard HTML or XHTML).

    The easiest way to go about implementing BBCode parsing is with a function in a file called functions.inc.php or something (.inc.php just makes it easier for you to spot and using an external file means it's easily portable).
    Something like:
    [php]function BBCode($input){
    /* The code goes here */
    }[/php]
    By using a function like that, you get portability (just include the file inside any other PHP script and you're off) and the ease of just typing:
    [php]<?php
    $input = "This is bold [color=#000000;]This is black[/color]";
    $output = BBCode($input);
    echo $output;
    ?>[/php]

    If you want the best speed, you would be better off running the BBCode parser before saving to the database.
    If you don't want to go to the trouble of writing some big HTML to BBCode parser, it might be worth having a database field for the parsed content and one for the raw content.
    When it comes to editing, you edit the raw version and your script implements the changes on the parsed version behind the scenes.
    That's how my current CMS system in Rails is doing it with Markdown instead of BBCode and it's working pretty well.
    PHP, CSS, XHTML, Delphi, Ruby on Rails & more.
    Current project: CMS Object.
    Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in.
    Release date: NEVER!!!
  • coolhomecoolhome Junior Member Shared Hoster
    Here is my bbcode function based off his preg.

    [php]
    <?php
    function bbcode($str) {
    $bbcodes = array("/\[b\](.+?)\[\/b\]/is",
    "/\[u\](.+?)\[\/u\]/is",
    "/\[i\](.+?)\[\/i\]/is",
    "/\[align\=(.+?)\](.+?)\[\/align\]/is",
    "/\[font\=(.+?)\](.+?)\[\/font\]/is",
    "/\[size\=(.+?)\](.+?)\[\/size\]/is",
    "/\[color\=(.+?)\](.+?)\[\/color\]/is",
    "/\[img\](.+?)\[\/img\]/is",
    "/\[img\=(.+?)\](.+?)\[\/img\]/is",
    "/\[url\](.+?)\[\/url\]/is",
    "/\[url\=(.+?)\](.+?)\[\/url\]/is",
    "/\[code\](.+?)\[\/code\]/is",
    "/\[mail\=(.*?)\](.*?)\[\/mail\]/is",
    "/\[mail\](.*?)\[\/mail\]/is",
    );
    $bbcodes_re = array("<strong>$1</strong>",
    "<u>$1</u>",
    "/\[i\](.+?)\[\/i\]/is",
    "<div style=\"text-align: $1;\">$2</div>",
    "<span style=\"font-family: $1;\">$2</span>",
    "<span style=\"font-size: $1;\">$2</span>",
    "<span style=\"color: $1;\">$2</span>",
    "<img src=\"$1\" alt=\"user posted image\"/>",
    "<img src=\"$2\" alt=\"$1\" title=\"$1\"/>",
    "<a href=\"$1\" target=\"_blank\">$1</a>",
    "<a href=\"$1\" target=\"_blank\">$2</a>",
    "<div style=\"background-color:#E9E9E9;border:1px solid #333333;padding:2px;width:95%;margin: auto;\"><code style=\"text-align:left;\">$1</code></div>",
    "<a href=\"mailto:$1\">$1</a>",
    "<a href=\"mailto:$1\">$2</a>",
    );
    $str = preg_replace_callback('#\[php\](.+?)\[/php\]#msi',create_function('$matches', 'return "<div style=\"background-color:#E9E9E9;border:1px solid #333333;padding:2px;width:95%;margin:auto;margin-bottom:3px;\"><code style=\"text-align:left;\">".highlight_string(_htmlspecialchars_decode($matches[1]), TRUE)."</code></div>";'), $str);
    $str = str_replace(chr(13).chr(10),"
    ",$str);
    $str = str_replace("
    ","<br />",$str);

    return preg_replace($bbcodes,$bbcodes_re,$str);
    }
    ?>
    [/php]
Sign In or Register to comment.