Announcement

Collapse
No announcement yet.

How do parsers work?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do parsers work?

    I've wanted to code my own custom CMS for my site for a long time and I've finally started with a login script, a confirm login status script, a log out script, and an add/remove user script. Anyway, I want to know how parsers work since I plan to code the capability to use BBCode in it.

    Would anyone be so kind to give me an example of how a simple parser would be coded to find/replace lets say the URL BBCode?

  • #2
    Parsers generally use preg_replace(), which is a function that uses PERL regular expressions to search and replace data, I'd suggest looking at this. But here's an example to parse the [.b] BBCode tag:

    PHP Code:
    $message $_POST'yourmessage' ];

    // Replace the open tag
    $message2 preg_replace'/\[b\]/''<strong>'$message );

    // Replace the close tag
    $message2 preg_replace'/\[\/b\]/''</strong>'$message2 ); 
    Edit: preg_replace() takes three required arguments: the pattern (the regular expression), the replacement, and then the string to perform the action on

    Hope this helps
    Last edited by daemon; Thu 26th Feb '04, 7:28pm.
    Bugdar: PHP bug tracking software that is beautiful, fast, and robust.

    Comment


    • #3
      The way you do it does not make very much sense - you could get the same result with str_replace which is much faster. I would use some code like this:

      PHP Code:
      $message preg_replace('/\[b\](.*)\[/b\]/''<strong>$1</strong>'$message); 
      This makes sure you have a open tag and a close tag...
      GameCrash

      Project Tools Importer for forum threads
      Current Version: 1.0.0 Beta 3

      Comment


      • #4
        Thanks for the code. ^_^

        Comment


        • #5
          Originally posted by GameCrash
          The way you do it does not make very much sense - you could get the same result with str_replace which is much faster. I would use some code like this:

          PHP Code:
          $message preg_replace('/\[b\](.*)\[/b\]/''<strong>$1</strong>'$message); 
          This makes sure you have a open tag and a close tag...
          PHP Code:
          $message preg_replace('/\[b\](.*)\[/b\]/i''<strong>$1</strong>'$message); 
          Would be better as it allows [B] and [b] (note the i in the first argument).
          vBulletin.org Moderator
          The Sisters Three Charmed Discussion Forums

          Comment


          • #6
            Cool, thanks. Also, one last thing, how would I know where to put the slashes?

            Comment


            • #7
              You'll wanna put the "/" slash as the first character and the character right after you're done with your expression part. The only thing that should go after the last slash in the first part of the preg_whatever statement is one of these things (though you don't have to use one of them )

              Basically, you want to use "\" characters for any character that you want to be used in regular every day typing. Granted, there are exceptions to that, so be sure to check out the link that was given to you earlier.

              I can imagine that's not very clear, but it's kinda hard to explain. All I'll say is that regex uses some characters in matching and whatnot, so you have to use a "\" to make it available for everyday use.

              In other news, the code that they gave you will give you an error. Try this:

              PHP Code:
              $message preg_replace('/\[b\](.*)\[\/b\]/i''<strong>$1</strong>'$message); 
              Note the \ before the /b
              Last edited by Silverwolf; Fri 27th Feb '04, 4:05pm.

              Comment


              • #8
                Also, it needs to be

                PHP Code:
                $message preg_replace('/\[b\](.*?)\[\/b\]/i''<strong>$1</strong>'$message); 
                As other wise it would match everything from the first opening tag to the last closing tag

                Comment


                • #9
                  In addition to the i switch at the end, you should also include the s switch, since this allows the .* (dot-star) to match newlines as well.

                  PHP Code:
                  $message preg_replace('#\[b\](.*)\[/b\]#si''<strong>$1</strong>'$message); 

                  Comment


                  • #10
                    Thanks for your help, I'm starting to code my parser now (school kept me from doing it two months ago) so I'm sure all of this will be very helpful.

                    Edit: One last question, how would I make this:

                    This is the first paragraph.

                    This is the second paragraph.

                    This is the third paragraph.
                    This is a simple linebreak.
                    Into this:

                    HTML Code:
                    <p>This is the first paragraph.</p>
                     
                    <p>This is the second paragraph.</p>
                     
                    <p>This is the third paragraph.<br />
                    This is a simple linebreak.</p>
                    I'm not quite sure so any help would be very appreciated.

                    Edit: I figured it out on my own, it was simplier than I thought:

                    PHP Code:
                    $text '<p>'.str_replace("\r\n\r\n"'</p><p>'$text).'</p>';
                     
                    $text '<p>'.str_replace("\r\n"'<br />'$text).'</p>'
                    Last edited by Shining Arcanine; Sat 10th Apr '04, 5:15pm.

                    Comment

                    widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                    Working...
                    X