PDA

View Full Version : Combining 2 preg_replace statements


Boofo
03-17-2003, 09:30 AM
Can someone please tell me how I can combine to 2 following statements into one? I need to parse both of them out of the same code but they seem to cancel each other out.

$reviewmessage = trim(preg_replace("/(\[)(sp)(])(\r\n)*(.*)(\[\/sp\])/siU", "<br><normalfont>Spoiler:</normalfont><br><font color=\"$backcolor\">\\5</font>", $post[pagetext]));

and:

$reviewmessage = trim(preg_replace("/(\[)(you)(])/siU", $bbuserinfo[username], $post[pagetext]));

Zzed
03-17-2003, 10:23 AM
The problem with regex is that you can match patterns with it, but you can't count with them. :(

The following pattern will do the match: ((.*)*(\[you\])*)*

The problem is that due to the repeating nature of the pattern and the fact that it has sub patterns you can not code a replacement for it. :(

You may be stuck with the 2 regex's...

Boofo
03-17-2003, 10:26 AM
I don't mind doing 2, but they cancel each other out. One will work, but not the other. Whichever one comes last seems to work. How do I make then both work?

Boofo
03-17-2003, 10:29 AM
You said this would do the match. Is this how it should look (with the siU)?

$reviewmessage = trim(preg_replace("((.*)*(\[you\])*)*", $bbuserinfo[username], $post[pagetext]));

Zzed
03-17-2003, 10:33 AM
Not sure if I am being picky here but in the second preg_replace you should change the regex

from "/(\[)(you)(])/siU"
to "/(\[)(you)(\])/siU"

I don't see why they would cancel each other out... :confused:

Zzed
03-17-2003, 10:35 AM
Today at 04:29 AM Boofo said this in Post #4 (https://vborg.vbsupport.ru/showthread.php?postid=367973#post367973)
You said this would do the match. Is this how it should look (with the siU)?

$reviewmessage = trim(preg_replace("((.*)*(\[you\])*)*", $bbuserinfo[username], $post[pagetext]));


What I meant was that the regex I gave would replace the (.*) in the first preg_replace and would try to match both patterns in your statements.

But since the replacements need to be done in 2 passes, it is of no help...

Boofo
03-17-2003, 10:40 AM
Today at 06:33 AM Zzed said this in Post #5 (https://vborg.vbsupport.ru/showthread.php?postid=367974#post367974)
Not sure if I am being picky here but in the second preg_replace you should change the regex

from "/(\[)(you)(])/siU"
to "/(\[)(you)(\])/siU"

I don't see why they would cancel each other out... :confused:

They don't cancel each other out. What is happening is that whichever ones comes last is the one that will work. The other one doesn't parse the code. Only the second (or last) one will.