Tags within tags and preg_replace

Want to chit chat about anything, do it here ... posting here won't increase your post count (or shouldn't!). Please do not post any "phpBB" specific topics here unless they do not fit into the category above. Do not post bug reports, feature or support requests!
Forum rules
Please do not post any "phpBB" specific topics here unless they do not fit into the category above.

Do not post bug reports, feature or support requests! No really... Do not post bug reports, feature or support requests! Doing so will make Bertie a very sad bear indeed. :(
Post Reply
User avatar
haravikk
Registered User
Posts: 292
Joined: Sun Apr 20, 2003 5:05 pm
Contact:

Tags within tags and preg_replace

Post by haravikk »

Okay, I am suffering no end to this horrible PRCE notation stuff but this one is probably something simple that should be possible but is beyond me to do at the moment.

I am trying to add switching capabilities to my site, now the system is very simple and the layout of a switch is as follows (note that the else is optional):

Code: Select all

<switch name>Display this if name is true</switch name><else name>Display this if not</else name>
You may notice that all elements are named, this is because I want them to nest correctly so that I can have further switches in an else block and so on.

This is where I am running into trouble. The particular HTML causing the problem is:

Code: Select all

<switch empty><switch cat>There are no link categories</switch cat><else cat>There are no links in this category</else cat></switch empty><else empty>{link_DESCRIPTION}</else empty>
You'll notice that there is a switch within a switch at the start. Now, since neither of these succeed (because neither the empty or cat variable is present in the code) then they are to be removed using the following code:

Code: Select all

$parse = preg_replace ("'<switch[^>]*?>.*?</switch[^>]*?>'si", '', $parse);
The trouble with this is that it removes the HTML from <switch empty> to </switch cat> instead of <switch empty> to </switch empty> as it should. What I want is for it to either:

1. Only remove a <switch name></switch name> block of code if the 'name' parts are identical
2. Only remove a block if it has no other blocks within it, that way it should remove the <switch cat></switch cat> block, THEN the <switch empty></switch empty> block.

Hopefully that didn't go completely over everybody's head and someone who knows this PRCE stuff better than me can offer a solution :)
Images in sigs! please.

User avatar
haravikk
Registered User
Posts: 292
Joined: Sun Apr 20, 2003 5:05 pm
Contact:

Re: Tags within tags and preg_replace

Post by haravikk »

From what I know of back referencing this PRCE expression should work:

Code: Select all

"'<switch([^>]*?)>.*?</switch\1>'si"
but it does not, it seems that PRCE notation isn't supporting the back referencing, is there a reason for this?

User avatar
psoTFX
Registered User
Posts: 1984
Joined: Tue Jul 03, 2001 8:50 pm
Contact:

Re: Tags within tags and preg_replace

Post by psoTFX »

\\1

Not Radio
Registered User
Posts: 159
Joined: Mon Oct 27, 2003 12:07 am
Contact:

Re: Tags within tags and preg_replace

Post by Not Radio »

or \1 if you are using simple quotes (').

User avatar
haravikk
Registered User
Posts: 292
Joined: Sun Apr 20, 2003 5:05 pm
Contact:

Re: Tags within tags and preg_replace

Post by haravikk »

psoTFX wrote:\\1
Hey thanks psoTFX! I must admit to spend hours trying to figure it out only to have you point out I missed a single backslash is a tag frustrating :X
Oh well, just a reminder who the PHP king around here is I suppose :)
Images in sigs! please.

Roberdin
Registered User
Posts: 1546
Joined: Wed Apr 09, 2003 8:44 pm
Location: London, United Kingdom

Re: Tags within tags and preg_replace

Post by Roberdin »

haravikk wrote:I must admit to spend hours trying to figure it out only to have you point out I missed a single backslash is a tag frustrating
We can see :P
Rob

User avatar
haravikk
Registered User
Posts: 292
Joined: Sun Apr 20, 2003 5:05 pm
Contact:

Re: Tags within tags and preg_replace

Post by haravikk »

Okay here's another one in relation to the switching mechanism I am using. I have the following code to remove any switches that are not triggered:

Code: Select all

<?php
	$clean = array(
			"'<switch([^>]*?)>.*?</switch\\1>'si",
			"'</switch[^>]*?>'i",
			"'<else[^>]*?>'i", "'</else[^>]*?>'i"
			);
	$parse = preg_replace ($clean, '', $parse);
?>
The problem is that if a switch contains a <br /> tag then the switch will be removed but the <br /> tag will remain, that or the switch is replaced by the <br /> tag, either way an undesired result. Any ideas as to why that happens?
Images in sigs! please.

User avatar
haravikk
Registered User
Posts: 292
Joined: Sun Apr 20, 2003 5:05 pm
Contact:

Re: Tags within tags and preg_replace

Post by haravikk »

Bah silly timed edit thingy.

Ignore that post I found it, it was nothing to do with the preg_replace after all
Images in sigs! please.

Post Reply