View Full Version : Resolved preg_replace confuses me.
MrEvil
11-10-2008, 12:34 AM
So, I have this:
$string = preg_replace("#\
(.+?)\[/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
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.
james438
11-10-2008, 08:44 AM
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 ;)
MrEvil
11-10-2008, 10:45 AM
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 (http://xs233.xs.to/xs233/08461/quote591.png) <===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.
james438
11-11-2008, 09:16 AM
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
helloBut 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.
james438
11-11-2008, 10:26 AM
After playing around with it a bit I came up with the following to start.
<?php
$string ="hi, how goes it this is
Bill
Bill meyer meyer 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.
MrEvil
11-11-2008, 10:29 AM
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- (http://www.regular-expressions.info/php.html). It's been pretty helpful. :D
MrEvil
11-11-2008, 10:30 AM
After playing around with it a bit I came up with the following to start.
<?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.
james438
11-11-2008, 10:42 AM
Above post has been fixed ;)
MrEvil
11-11-2008, 11:06 AM
Awesomesauce. Works like a charm. Thanks so much, man.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.