Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: BBcode help? :)

  1. #1
    Join Date
    Sep 2008
    Location
    UK
    Posts
    3,670
    Tokens
    0

    Latest Awards:

    Default BBcode help? :)

    Hey, I'm making a little news system and the last thing I need to do is include the BBCode. I read that I could use str_replace to do this? How would I set about doing it?
    Back for a while.

  2. #2
    Join Date
    Jan 2006
    Location
    Kent
    Posts
    987
    Tokens
    0

    Default

    http://w3schools.com/php/func_string_str_replace.asp

    If I was you, i'd just insert the original data into the database, then convert it when displaying it. I'm not sure if converting it then inputting would be quicker, I imagine so, but meh.
    This is our situation and we're happy to be here,
    I wouldn't change this place for anything.


  3. #3
    Join Date
    Dec 2006
    Posts
    3,970
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by Joe! View Post
    http://w3schools.com/php/func_string_str_replace.asp

    If I was you, i'd just insert the original data into the database, then convert it when displaying it. I'm not sure if converting it then inputting would be quicker, I imagine so, but meh.
    You would need to use preg_replace otherwise someone could do [b] and leave it open and the rest of the page would be in bold.

    PHP Code:
    $string "[b]This[/b] text is in [b]bold[/b]";
    preg_replace("/[b](.*?)[/b]/""<b>$1</b>"$string); 
    That may not be 100% right but give it a go.
    Lets set the stage on fire, and hollywood will be jealous.

  4. #4
    Join Date
    Sep 2008
    Location
    UK
    Posts
    3,670
    Tokens
    0

    Latest Awards:

    Default

    Yeah I was thinking that way but I just wanted to say include the file onto the index page so when say.. <b>Hiya</b> is displayed it automatically changes it to bold.
    Back for a while.

  5. #5
    Join Date
    Apr 2006
    Location
    London, England
    Posts
    696
    Tokens
    0

    Default

    You can make a quick function like this:

    Code:
    function bbcode($content)
     {
     $content = preg_replace(”\[b\](.+?)\[\/b\]”, “<b>$1</b>”, $content);
     $content = preg_replace(”\[i\](.+?)\[\/i\]”, “<i>$1</i>”, $content);
     $content = preg_replace(”\[u\](.+?)\[\/u\]”, “<u>$1</u>”, $content);
     $content = preg_replace(”\[img\](.+?)\[\/img\]”, “<img src=\”$1\” alt=\”\” />”, $content);
     return($content);
     }
    Last edited by DUB; 26-09-2008 at 08:35 PM.
    ;veni vidi vici
    ;i came, i saw, i ownt

  6. #6
    Join Date
    Jan 2006
    Location
    Kent
    Posts
    987
    Tokens
    0

    Default

    I get this when doing DUB's method.
    Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\User system revision\functions.php on line 64

    Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\User system revision\functions.php on line 65

    Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\User system revision\functions.php on line 66

    Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\User system revision\functions.php on line 67
    This is our situation and we're happy to be here,
    I wouldn't change this place for anything.


  7. #7
    Join Date
    Apr 2006
    Location
    London, England
    Posts
    696
    Tokens
    0

    Default

    oh yeah my bad

    PHP Code:
    function bbcode($content)
     {
     
    $content preg_replace(/[b\](.+?)/[\/b\]<b>$1</b>$content);
     
    $content preg_replace(/[i\](.+?)/[\/i\]<i>$1</i>$content);
     
    $content preg_replace(/[u\](.+?)/[\/u\]<u>$1</u>$content);
     
    $content preg_replace(/[img\](.+?)/[\/img\]<img src=\$1\” alt=\\” />$content);
     return(
    $content);
     } 
    ;veni vidi vici
    ;i came, i saw, i ownt

  8. #8
    Join Date
    Sep 2008
    Location
    UK
    Posts
    3,670
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by DUB View Post
    oh yeah my bad

    PHP Code:
    function bbcode($content)
     {
     
    $content preg_replace(/[b\](.+?)/[\/b\]<b>$1</b>$content);
     
    $content preg_replace(/[i\](.+?)/[\/i\]<i>$1</i>$content);
     
    $content preg_replace(/[u\](.+?)/[\/u\]<u>$1</u>$content);
     
    $content preg_replace(/[img\](.+?)/[\/img\]<img src=\$1\” alt=\\” />$content);
     return(
    $content);
     } 
    Parse error: syntax error, unexpected '=', expecting ')' in /home/luke/public_html/syst/bbcode.php on line 5
    Back for a while.

  9. #9
    Join Date
    Apr 2006
    Location
    London, England
    Posts
    696
    Tokens
    0

    Default

    oh crap

    PHP Code:
    function bbcode($content)
    {
     
    $content preg_replace('/\[b\](.*?)\[\/b\]/is''<strong>$1</strong>'$content);
     
    $content preg_replace('/\[i\](.*?)\[\/i\]/is''<em>$1</em>'$content);
     
    $content preg_replace('/\[u\](.*?)\[\/u\]/is''<u>$1</u>'$content);
     
    $content preg_replace('/\[img\](.*?)\[\/img\]/is'
    '<img src="$1" />'$content);

    return(
    $content);

    that should work
    ;veni vidi vici
    ;i came, i saw, i ownt

  10. #10
    Join Date
    Sep 2008
    Location
    UK
    Posts
    3,670
    Tokens
    0

    Latest Awards:

    Default

    Doesn't work when I apply the <strong></strong> tags.
    Back for a while.

Page 1 of 2 12 LastLast

Posting Permissions

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