Using BBcodes in my php script.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    Using BBcodes in my php script.

    Many forums and also Joomla make use of BBcodes to allow us to Bold and italic in the posts.

    How can I find out if I have this capability on my server ?
    Is there something in the php.ini that shows that the required extension
    is loaded ?


    I believe that BBcodes makes use of an extension package found at PECL :: Package :: bbcode. It also may have something to do
    with PEAR ?
    I don't want to try and install something that may already be on
    my server.

    Is anyone familair with this ?
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You could use function_exists () on any of the parser functions to see if they exist (ie. the extension is available).

    PHP: Installing/Configuring - Manual

    By the way, it's very simple to write your own parser. Very simple.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      Originally posted by jeddiki
      It also may have something to do with PEAR ?
      there is also a BBCode package in the PEAR repository, but that's a different one.

      Comment

      • jeddiki
        Contributor
        • Jan 2009
        • 290

        #4
        Thanks,
        I was going to write my own parser but the extension is supposed to be a lot quicker than using ereg's.

        Having said that - I just looked at the parser functions - looks like I've got a load more stuff to learn if I go that route.

        Maybe I'LL just stick to normal php try and write a parser for what I want to do.

        All I need really is to parse for bold and italics plus [L] [/L]
        for link (href)

        It would be nice to have a little javascript on the page to press buttons to insert
        these tags - but that could be a latter add-on.

        Instead of eregs can I just use:
        str_replace(fin d,replace,strin g,count)

        so str_replace([b], ???,$art_body)

        Just thought - what do I need to put into a string for bold that is going to put stored in a mysql database it must be & something ?

        same with 'close bold' and italics and links. If someone can help me out with this, probably I can do it :)

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          All you need, really, to parse the simple tags like [b] and [i] is a simple str_replace.
          [code=php]
          $old = array('[b]', '[/b]');
          $new = array('<strong> ', '</strong>');
          $outText = str_replace($ol d, $new, $inText);[/code]
          This would be enough to deal with bold tags. Adding italic or underlined would be as simple as adding them to the $old and $new arrays.

          To parse links, you would probably have to go with a simple regular expression.
          I'm guessing there are plenty of examples on that available via Google.

          This should ideally be done only when you are displaying the text.
          Don't parse the text before you put it into the database. Do it when you show it. It's just far easier to maintain that way.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Also, the standard BBCode for links is [URL=http://someplace.com]Link text[/URL] or [URL]http://someplace.com[/URL]. Just an FYI.

            Comment

            • jeddiki
              Contributor
              • Jan 2009
              • 290

              #7
              Oh - I see what you mean.

              The bracketed B and I and U and URL can all stay as they are in the Database then ?

              I only need to change them after reading them back out and before displaying them ?

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Yes, that's usually the best way to do it.

                Generally, it's best to store data in it's original form. That way, if you ever change the way you display it, you won't have to re-format all the data already inside your database. You would just change the way your front-end displays it.

                Comment

                • jeddiki
                  Contributor
                  • Jan 2009
                  • 290

                  #9
                  Thanks for your help :)

                  Comment

                  Working...