preg_match backreference to find ending tag

前提是你 提交于 2020-01-06 19:02:05

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/13016458/preg-match-backreference-to-find-ending-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!