Jump to content

Problem matching certaing pattern in template


TeNDoLLA

Recommended Posts

Hi,

I am trying to match an if-else patterns in templates and below is the closest I've gotten. It works if there is only one if-else in the template. But if there is more it will get everything starting from first IF to the last ENDELSE. I would like to capture the data for each if-else in the array.

 

HTML example 1:

<?php
$html = '<div>
{:if([:is_admin:]):} IS ADMIN {:endif:}
{:else:} NOT AN ADMIN {:endelse:}
</div>';

 

PHP example:

<?php
preg_match_all('/\{:if\((.*?)\):\}(.*?)\{:endif:\}.*\{:else:\}(.*?)\{:endelse:\}/s', $html, $matches, PREG_SET_ORDER);

 

Result 1:

Array
(
    [0] => {:if([:is_admin:]):} IS ADMIN {:endif:}
{:else:} NOT AN ADMIN {:endelse:}
    [1] => [:is_admin:]
    [2] =>  IS ADMIN 
    [3] =>  NOT AN ADMIN 
)

 

Now what I have here is:

0 = string to replace.

1 = variable to check for true/false

2 = text to put in place if true

3 = text to put in place if false

 

Now if there is more than one if-else I get the whole string starting from first if to the last endelse. Say for example

<?php
$html = '<div>
{:if([:is_admin:]):} IS ADMIN {:endif:}
{:else:} NOT AN ADMIN {:endelse:}
</div>

<div>
{:if([:another:]):} ANOTHER TRUE {:endif:}
{:else:} ANOTHER FALSE {:endelse:}
</div>';

 

It won't work anymore. And I am not able to get the regexp that way it would capture all occurances to the array separately. Anyone able to help me with this?

Link to comment
Share on other sites

Thanks for reply Mark, I looked your example and because of the very different tags and structre you are using it was a little bit hard to read for me. I managed to solve the problem with adding one question mark between the if / else statements.

 

In the middle of the regexp this part was before:

{:endif:\}.*\{:else:\}

 

and after (not greedy matching everything between the clauses)

{:endif:\}.*?\{:else:\}

 

Thanks for looking it though! Was almost the same thing as you were doing. Now the results are as I want them to be. Cheers.

Link to comment
Share on other sites

Actually now a new problem popped up. Now it matches multiple else-if's but if there is just a plain if without the else somewhere between it screws up. I should also make it so that it would match only else-if's and not plain if's. Tried adding there a negated character class for testing: [^:if] before the last endelse without success. I am not very expert with regular expressions. Anyone could point me out how can I add a pattern inside that regexp that must not be matched in certain parts? Certain parts being after the if started till the end facing endelse. After that there could be again new if-else starting.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.