BBCode/HTML filters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    BBCode/HTML filters

    Another thread sparked an interest:
    With using BBCode, do you have to install that package or is it the browser that recognises it? I would like to make my own for my website to let certain users edit the code (without needing to know html) and so I was just wondering where I should look. Most of the links refer to phpBB forums, but this will be using my own code on my site.

    Alternatively, if this is not available, do any of you know a good free html filter (which is basically what BBCode is I believe)?
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by TheServant
    Another thread sparked an interest:
    With using BBCode, do you have to install that package or is it the browser that recognises it? I would like to make my own for my website to let certain users edit the code (without needing to know html) and so I was just wondering where I should look. Most of the links refer to phpBB forums, but this will be using my own code on my site.

    Alternatively, if this is not available, do any of you know a good free html filter (which is basically what BBCode is I believe)?
    You can install it, or create your own. It's nothing to do with the browser.

    You just turn [b] text [/b] into <b> text </b> using regular expressions.

    :)

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      It is very simple to implement the basic BBCode tags yourself, like Marcus mentioned.

      You could simply use str_replace to replace the BBCode tags with their HTML counterparts.

      For example, for bold and italic text:
      [code=php]
      $BBCodeTags = array("&#91;b]", "&#91;/b]", "&#91;i]", "&#91;/i]");
      $HTMLTags = array("<strong> ", "</strong>", "<em>", "</em>");
      $out = str_replace($BB CodeTags, $HTMLTags, $in);
      [/code]
      Other, more complex tags may require regular expressions, like the &#91;url] tags:
      [code=php]
      $regex = '#\&#91;url\](.*?)\&#91;\/url\]#is';
      $replace = '<a target="_blank" href="$1">$1</a>';
      $out = preg_replace($r egex, $replace, $in);
      [/code]
      You get the point.

      If your not up to that, there is a PEAR class and probably some free PHP classes available.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by Atli
        It is very simple to implement the basic BBCode tags yourself, like Marcus mentioned.
        Markus with a k, my friend.

        ;]

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by markusn00b
          Markus with a k, my friend.
          Sorry mate... accidentally wrote it with a small M and then clicked the wrong item in the Firefox word-hint menu :P

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            That's fantastic guys. I was going to do it that way, but I didn't know you could use str_replace with arrays! Anyway, that will be much easier. I need to practice with regex though, as somethings I want to do could be complex. But you have given me lots to work on thanks!

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Originally posted by TheServant
              That's fantastic guys. I was going to do it that way, but I didn't know you could use str_replace with arrays! Anyway, that will be much easier. I need to practice with regex though, as somethings I want to do could be complex. But you have given me lots to work on thanks!
              Regexps are awful! But necessary.

              Keep on rockin'!

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Yea. Regex can be very annoying. Too bad they made them so damn useful :)

                Happy coding! 8)

                Comment

                • TheServant
                  Recognized Expert Top Contributor
                  • Feb 2008
                  • 1168

                  #9
                  Originally posted by Atli
                  [code=php]
                  $regex = '#\[url\](.*?)\[\/url\]#is';
                  $replace = '<a target="_blank" href="$1">$1</a>';
                  $out = preg_replace($r egex, $replace, $in);
                  [/code]
                  I am confused. looked at quite a few regex websites, but none are complete with knowledge. Do you guys have a recommended regex site with all the commands?

                  I have some questions about this:
                  Isn't the # indicating commenting?
                  What is the "is" on the end of line 2?

                  If I am asking the wrong questions, let me know a site and I will work it out because these more involved regex ones I have never spent any time on. Thanks again.

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    #10
                    Originally posted by TheServant
                    I have some questions about this:
                    Isn't the # indicating commenting?
                    What is the "is" on the end of line 2?
                    The # there marks the beginning and end of the regex itself.
                    The / char is typically used, but you can use other chars if you chose to do so. The # char is just a personal preference of mine.

                    The same regex could have been written like so and it would have made no difference.
                    Code:
                    /\[url\](.*?)\[\/url\]/is
                    The 'i' and 's' at the end are options.
                    The 'i' makes the search case-insensitive, while the 's' includes the new-line char in the dot-class, effectively making the search capable of spanning multiple lines.

                    Check out Regular-Expressions.inf o. Lots of useful info there.

                    Comment

                    • TheServant
                      Recognized Expert Top Contributor
                      • Feb 2008
                      • 1168

                      #11
                      Thanks mate. I also found this one which I printed for further reference. Thought i should share it!

                      Comment

                      Working...