Results 1 to 9 of 9

Thread: preg_replace confuses me.

  1. #1
    Join Date
    Nov 2008
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default preg_replace confuses me.

    So, I have this:
    PHP Code:
    $string preg_replace("#\[quote\](.+?)\[/quote\]#is""<blockquote>\\1</blockquote>"$string); 
    And it works fine, except for one thing: If you quote a post that already has a quote, it shows up like so:
    Text text text
    [**QUOTE]More text
    [/QUOTE]
    I guess there's two options. Filter out all quotes except for the newest, or parse the [quote] tags multiple times. Either one would make me a very happy person, but I'm not sure how to do either.

    Thanks much.
    Last edited by MrEvil; 11-11-2008 at 10:23 AM.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    hmm, you have some interesting syntax for your preg_replace.

    I might not be understanding you just yet. Could you post a sample text and another sample text showing what it should look like after the text is processed by the preg_replace? I think I know what you want and need, but not totally sure yet
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Nov 2008
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Probably so. A friend wrote it a long time ago. I would've asked him about it, but I haven't been able to get in touch with him.

    Screenshot <===link. That's what happens when there's multiple quotes within a post. What I would like to happen is for either the original quote to be filtered out, or for it to be interpreted/parsed as HTML, like it does on the first one.

    Thanks for responding. : )

    EDIT: I found a fix (an extra (.+?) before #is), but that also somehow knocked off the first letter of the post. str_replace seems to work just as well for what I'm doing, and simpler. I'll stick with that for now. I'm open to any ideas about the original problem, since I should probably learn more about regex. w00t.
    Last edited by MrEvil; 11-10-2008 at 11:22 AM.

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    For what you are doing str_replace is definitely the way to go. It took me a while to figure out what you were trying to do. PCRE is rather taxing on the server so it is best to use it only when you really need it and in this you don't really need it .

    I am glad I did not respond right away, because after thinking about it I really was misunderstanding what you were asking and would have given you the wrong advice. As far as how to do what you are asking with PCRE I don't know how to do it off hand. I would have to think on it a while longer before I figured it out. I am sure this problem would be a lot easier for someone else though. Why would you want to know how to do this with PCRE? Well, you could use it to do things like:

    hello
    hello
    hello
    hello
    But it sounds like fun, so I'll see if I can figure out the PCRE for what you are asking even though you don't really need it anymore.

    EDIT: On second thought I wouldn't need PCRE even then. Oh, well. Still, this is good practice though.
    Last edited by james438; 11-11-2008 at 10:17 PM.
    To choose the lesser of two evils is still to choose evil. My personal site

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    After playing around with it a bit I came up with the following to start.

    PHP Code:
    <?php
    $string 
    ="hi, how goes it this is [quote]Bill[quote]Bill meyer[/quote] meyer[/quote] meyer";
    $stringo preg_replace('/\[quote\](?!.*?\[quote\]).*?\[\/quote\]/is'"XXXXXX"$string); 
    print 
    $stringo;
    echo
    "<br>$string";
    ?>
    EDIT: Grr, I messed up somewhere. Just a sec.
    EDIT: kk, fixed.

    loop the string with the above PCRE the number of times that there are nested "quotes text /quotes" and your string will be modified appropriately. This is, of course, silly since str_replace will do the same thing faster and is easier on your server, but it is good to work on these things to stay in practice.
    Last edited by james438; 11-11-2008 at 11:18 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

  6. The Following User Says Thank You to james438 For This Useful Post:

    MrEvil (11-11-2008)

  7. #6
    Join Date
    Nov 2008
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    I've gotta agree. The one who originally started the script did all the BBCodes using preg_replace. I'm going through it now to re-do it using str_replace. Though, in the process of thinking preg_replace was somehow better, I found -this-. It's been pretty helpful.

  8. #7
    Join Date
    Nov 2008
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by james438 View Post
    After playing around with it a bit I came up with the following to start.

    PHP Code:
    <?php
    $stringa 
    ="hi, how goes it this is  meyer";
    $stringb preg_replace('/\[quote\](?!.*?\[quote\]).*?\[\/quote\]/'"$0"$string); 
    print 
    $stringb;
    echo
    "<br>$stringa";
    ?>
    Grr, I messed up somewhere. Just a sec.
    Sweet. I haven't tried it yet, so I'm not sure where you messed up. I'll play with it in a bit.

  9. #8
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Above post has been fixed
    To choose the lesser of two evils is still to choose evil. My personal site

  10. #9
    Join Date
    Nov 2008
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Awesomesauce. Works like a charm. Thanks so much, man.

Bookmarks

Posting Permissions

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