Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: BB Code

  1. #1
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts

    BB Code

    I need to do a BB code thing for a guestbook I am making. I also need to disable html. I don't really know where to start. I know I need to do something like:
    PHP Code:
    preg_replace($_POST['comments'], "[b]""<b>"
    But that doesn't work. I have an idea with that. But I have absolutely no idea how to disable html...
    How would I do this?

    All help is apreciated

    note: I tried searching the forum but it said that BB was too short...

  • #2
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you want to disable all HTML, just use strip_tags();
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #3
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    I like htmlentities() better...

    Code:
    $data = preg_replace("/\[b\]/","<strong>",$data);
    $data = preg_replace("/\[\/b\]/","</strong>",$data);
    Simple enough, follow that pattern.. You could optimize it more, make it pull the patterns from mysql or text file and have dynamic bb even.
    Omnis mico antequam dominus Spookster!

  • #4
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Mhtml
    I like htmlentities() better...

    Code:
    $data = preg_replace("/\[b\]/","<strong>",$data);
    $data = preg_replace("/\[\/b\]/","</strong>",$data);
    Simple enough, follow that pattern.. You could optimize it more, make it pull the patterns from mysql or text file and have dynamic bb even.
    What are htmlentities() ?

  • #5
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts

  • #6
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    It's a good idea to grab the CHM version of the PHP reference as well, saves lots of time.
    Omnis mico antequam dominus Spookster!

  • #7
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I did look that up, I just couldn't understand it...

  • #8
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    The htmlentities() function?

    Ok, say you have a string with html in it .. You might want for whatever reason to strip the html so that it just becomes plain text. htmlentities does this for you!
    PHP Code:
    $string "<script>while(1)alert('this is a malice natured script that someone could post in a forum or similar that allowed html!');</script>";

    $string htmlentities($string);
    echo 
    $string
    Now, the string which you echo will now look like -
    <script>while(1)alert('this is a malice natured script that someone could post in a forum or similar that allowed html!');</script>

    Rather than being real html which would continually popup alert boxes .. Which is why most places strip html, or at least the script tags.
    Omnis mico antequam dominus Spookster!

  • #9
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, it works great. It is just the manual that php.net gives you can be hard for me to understand, I usually go there before I go to here though, I am just not expierenced enough to really understand how they do that

  • #10
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yep it usually takes a while to get used to the documentation... I find it helps to read the page (so you can find out what the function does and what arguments it has ect), then have a go at the examples they provide in a test php file. (not saying theres any problem with you posting here, just trying to help you find a faster solution in the future )

  • #11
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, I have a new thing that works great, except there is a problem, and I have no idea what I am doing worong or where it would say in the manual...

    I have a break tag ([break]) that I preg_replace to <br> and it works except all text after [break] goes on a new line, not the same on, I have absolutely no idea why...

  • #12
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmm... im not sure I quite understand your last post... If you replace [break] with <br> then the code will go on a new line becuase the <br /> tag starts a newline...

  • #13
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no, the source, not the text itself...

    Example:
    it prints this
    PHP Code:
    code....<br>
    hello 
    instead of
    PHP Code:
    code...<br>hello 
    Maybe that will clear things up...
    Last edited by stophon4; 04-30-2004 at 09:31 PM.

  • #14
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmm... It may be that when you input text, you are leaving a newline before putting the [break] tag.

    eg:

    Code:
    Blah....
    [break]
    Blah...
    This means that your return spaces are actually being sent as \r\n (or just \n if you are on linux), and in the source this will appear as a newline. You could consider using nl2br ( http://www.php.net/nl2br ) to replace newlines with <br /> tags?

  • #15
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Can I see your preg_replace call for the [break] code, perhaps there is something wrong with it a little.
    Omnis mico antequam dominus Spookster!


  •  
    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
    •