0

I'm trying to use regex to parse a template I have. I want to find the _stop tag for the _start tag. I need to find the specific ones since there can be nested _stop and _start tags.

The regex I'm using is

/{(.*?)_start}.*{(\1_stop)}/s

and throwing that into a preg_match

And the template

<div data-role="collapsible-set" class="mfe_collapsibles" data-theme="c" data-inset="false">
        {MakeAppointment_start}
        <div id="appointmentHeading" data-action-id="appointmentNext" data-action-text="Next" data-a data-role="collapsible" data-collapsed="true" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d" data-iconpos="right">
            <h3 class="collapsibleMainHeading">New {AppointmentTerm}</h3>
            <p>
                {AppointmentForm}
            </p>
        </div>
        {MakeAppointment_stop}
        {RegisterSection_start}
        <div id="registerHeading" class="preRegistration" data-action-id="register" data-action-text="Register" data-role="collapsible" data-collapsed="true" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d" data-iconpos="right">
            <h3 class="collapsibleMainHeading">Register</h3>
            <p>
                {RegisterForm}
            </p>
        </div>
        {RegisterSection_stop}
        <div data-role="collapsible" class="preRegistration" data-collapsed="true" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d" data-iconpos="right">
            <h3 class="collapsibleMainHeading">Login</h3>
            <p>
                {LoginForm}
            </p>
        </div>

    </div>
</div>

The results are

Array
(
    [0] => {MakeAppointment_start}
        <div id="appointmentHeading" data-action-id="appointmentNext" data-action-text="Next" data-a data-role="collapsible" data-collapsed="true" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d" data-iconpos="right">
            <h3 class="collapsibleMainHeading">New {AppointmentTerm}</h3>
            <p>
                {AppointmentForm}
            </p>
        </div>
        {MakeAppointment_stop}
    [1] => MakeAppointment
    [2] => MakeAppointment_stop
)

Index 0 is correct however 1 and 2 are not. 1 should have the register tags and content and 2 should not exist.

What am I doing wrong here?

Bot
  • 11,868
  • 11
  • 75
  • 131

1 Answers1

2

Firstly, preg_match only returns one match. use preg_match_all instead. Secondly, the indices 1 and 2 you get, are your capturing groups. You can simply ignore them, although your second capturing group is quite redundant; you could just remove the second pair or parentheses in your regex. Using preg_match_all will yield the full match and all capturing groups for all matches.

I also think you should escape your { and } since they are regex meta-characters. I wonder why the engine does not choke on them this way, but I think it is better practice to just escape them anyway.

Martin Ender
  • 43,427
  • 11
  • 90
  • 130
  • actually the engine chokes on them with them being escaped. PHP doc said if they are escaped, they will be taken as literal chars. – Bot Oct 22 '12 at 17:31
  • But you want them to be literal characters. What exactly happens if you escape them? Alternatively, can you post your `preg_match` call? – Martin Ender Oct 22 '12 at 17:33