我有一个适用于一小部分代码的正则表达式,但是在整个页面上运行它时,它只返回第一个匹配项。为什么?
我的问题与preg_match 反向引用以查找结束标签有关。但现在我有一个不同的问题。
来自以下代码的正则表达式
/\{(.*?)_start\}.*\{\1_stop\}/s
Preg_match_all
$loopPattern = "/\\$this->tokenStartDelimiter(.*?)_start\\$this->tokenStopDelimiter.*\\$this->tokenStartDelimiter\\1_stop\\$this->tokenStopDelimiter/s";
preg_match_all($loopPattern, $this->template, $matches);
foreach ($matches[0] as $match) {
$this->template = str_replace($match, '', $this->template);
}
整页
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<title>Appointment-Plus</title>
<!--<link rel="stylesheet" href="css/themes/default/jquery.mobile.theme-1.2.0.min.css"> -->
<link rel="stylesheet" href="css/themes/apcv/APCV-Theme.min.css">
<link rel="stylesheet" href="css/themes/default/jquery.mobile.structure-1.2.0.min.css">
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.2.0.min.js"></script>
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
<script type="text/javascript" src="js/pages/all.js"></script>
<link rel="stylesheet" href="css/mfe.css">
</head>
<body>
<div data-role="page" data-theme="d">
<!-- Some icons by Yusuke Kamiyamane. -->
<div data-id="head" data-role="header" data-position="fixed" data-theme="b" data-tap-toggle="false">
<h1>DB Games</h1>
<a href="#" id="headingsActionButton" class="ui-btn-right headingsActionButton hidden" data-role="button" data-inline="true" data-icon="check">Save</a>
</div>
<div id="mainContent" data-role="content">
<!-- <div data-role="fieldcontain">
<div style="width: 100%; height: 100px; position: relative; background-color: #fbfbfb; border: 1px solid #b8b8b8;">
<img src="https://codiqa.com/static/images/v2/image.png" alt="image" style="position: absolute; top: 50%; left: 50%; margin-left: -16px; margin-top: -18px;">
</div>
</div> -->
<div data-role="fieldcontain">
<div data-role="collapsible-set" class="mfe_collapsibles" data-theme="c" data-inset="false">
<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 Appointment</h3>
<p>
<script src="js/pages/newAppointment.js"></script>
<div data-role="collapsible-set" data-content-theme="c" class="appointmentMakeCollapsibleSet">
<div data-role="collapsible" data-collapsed="false" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d">
<h3>Appointment</h3>
<p>
<form id="appointmentPrimaryForm">
<label for="c_id" class="select required" required>Location: </label>
<select name="c_id" id="c_id" data-native-menu="true" class="select required" required>
<option value="" data-placeholder="true">Choose One</option>
<option value="261"
selected>Mesa</option>
<option value="1007"
>Chandler</option>
</select>
{AppointmentStaffSelect_start}
<label for="apptCreateEditStaff" class="select required" required>With: </label>
<select name="apptCreateEditStaff" id="apptCreateEditStaff" data-native-menu="true" class="select required" required>
<option value="" data-service="" data-placeholder="true">Choose One</option>
<option data-service="{Services}" value="5490"
>Brian</option>
<option data-service="{Services}" value="4388"
>Mckenna</option>
<option data-service="{Services}" value="4387"
>Katelyn</option>
<option data-service="{Services}" value="783"
>David</option>
<option data-service="{Services}" value="4389"
>Ryan</option>
<option data-service="{Services}" value="4394"
>Ryan - c</option>
<option data-service="{Services}" value="784"
>Tami</option>
<option data-service="{Services}" value="4390"
>Jake</option>
</select>
{AppointmentStaffSelect_stop}
<p>{Test}</p>
{AppointmentTest_start}
<p></p>
{AppointmentTest_stop}
<label for="service_id" class="select required">Service: </label>
<select name="service_id" id="service_id" data-native-menu="true" class="select required" required>
<option value="" data-placeholder="true" data-duration="0" data-cost="0.00">Choose One</option>
<option value="1174|30 Minute Gaming Pass" class="{Classes}" data-cost="3.00" data-duration="30">30 Minute Gaming Pass</option>
<option value="1176|1 Hour Gaming Pass" class="{Classes}" data-cost="5.00" data-duration="60">1 Hour Gaming Pass</option>
<option value="1177|2 Hour Gaming Pass" class="{Classes}" data-cost="7.00" data-duration="120">2 Hour Gaming Pass</option>
</select>
{AppointmentAddonService_start}
<fieldset data-role="controlgroup" id="apptCreateEditAddonServicesFieldset">
<legend>Add-on Service(s):</legend>
<input type="checkbox" name="aos-5059" id="aos-5059" data-duration="30" data-cost="10.00" class="custom {Classes}" />
<label for="aos-5059" class="{Classes}">Unlimited Snack Bar</label>
<input type="checkbox" name="aos-5060" id="aos-5060" data-duration="30" data-cost="5.00" class="custom {Classes}" />
<label for="aos-5060" class="{Classes}">Unlimited Energy Drinks</label>
<input type="checkbox" name="aos-5061" id="aos-5061" data-duration="30" data-cost="1.00" class="custom {Classes}" />
<label for="aos-5061" class="{Classes}">Candy Bar</label>
</fieldset>
{AppointmentAddonService_stop}
<label for="apptCreateEditDate" class="select required" required>Date: </label>
<select name="apptCreateEditDate" id="apptCreateEditDate" data-native-menu="true" class="select required" required>
<option value="" data-placeholder="true">Choose One</option>
{apptCreateEditDateOption_start}
<option value="{Value}"
{Selected}>{Name}</option>{apptCreateEditDateOption_stop}
</select>
<label for="apptCreateEditTime" class="select required" required>Time: </label>
<select name="apptCreateEditTime" id="apptCreateEditTime" data-native-menu="true" class="select required" required>
<option value="" data-placeholder="true">Choose One</option>
{apptCreateEditTimeOption_start}
<option value="{Value}"
{Selected}>{Name}</option>{apptCreateEditTimeOption_stop}
</select>
</form>
</p>
</div>
<div data-role="collapsible" data-collapsed="true" class="ui-disabled" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d">
<h3>Children</h3>
<p>
Page 2
</p>
</div>
<div data-role="collapsible" data-collapsed="true" class="ui-disabled" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d">
<h3>Appointment Details</h3>
<p>
<label for="customer_notes">Special Instructions for Customer: </label>
<textarea cols="40" rows="8" name="customer_notes" id="customer_notes"></textarea>
{apptCreateEditPONumber_start}
<label for="apptCreateEditPONumber" class="{Class}">{LName}: </label>
<input type="text" id="apptCreateEditPONumber" name="apptCreateEditPONumber" class="{Class}" value=""/>
{apptCreateEditPONumber_stop}
{apptCreateEditVehicleMake_start}
<label for="apptCreateEditMakeId" class="select {Class}">{LName}: </label>
<select name="apptCreateEditMakeId" id="apptCreateEditMakeId" data-native-menu="true" class="select {Class}">
<option value="" data-placeholder="true">Choose One</option>
{apptCreateEditMakeOption_start}
<option value="{Value}"
{Selected}>{Name}</option>{apptCreateEditMakeOption_stop}
</select>
{apptCreateEditVehicleMake_stop}
{apptCreateEditVehicleModel_start}
<label for="apptCreateEditModelId" class="select {Class}">{LName}: </label>
<select name="apptCreateEditModelId" id="apptCreateEditModelId" data-native-menu="true" class="select {Class}">
<option value="" data-placeholder="true">Choose One</option>
{apptCreateEditModelOption_start}
<option value="{Value}" class="{Make}"
{Selected}>{Name}</option>{apptCreateEditModelOption_stop}
</select>
{apptCreateEditVehicleModel_stop}
{apptCreateEditVehicleYear_start}
<label for="apptCreateEditModelYear" class="{Class}">{LName}: </label>
<select name="apptCreateEditModelYear" id="apptCreateEditModelYear" data-native-menu="true" class="select {Class}">
<option value="" data-placeholder="true">Choose One</option>
{apptCreateEditYearOption_start}
<option value="{Value}"
{Selected}>{Name}</option>{apptCreateEditYearOption_stop}
</select>
{apptCreateEditVehicleYear_stop}
{apptCreateEditVehicleOther_start}
<label for="apptCreateEditOtherVehicle" class="{Class}">{LName}: </label>
<input type="text" id="apptCreateEditOtherVehicle" name="apptCreateEditOtherVehicle" class="{Class}" value="{ApptCreateEditVehicleOther}"/>
{apptCreateEditVehicleOther_stop}
{apptCreateEditVehicleVIN_start}
<label for="apptCreateEditVIN" class="{Class}">{LName}: </label>
<input type="text" id="apptCreateEditVIN" name="apptCreateEditVIN" class="{Class}" value="{ApptCreateEditVehicleVIN}"/>
{apptCreateEditVehicleVIN_stop}
{apptCreateEditVehicleOdometer_start}
<label for="apptCreateEditOdometer" class="{Class}">{LName}: </label>
<input type="number" id="apptCreateEditOdometer" name="apptCreateEditOdometer" class="{Class}" value="{ApptCreateEditVehicleOdometer}"/>
{apptCreateEditVehicleOdometer_stop}
</p>
</div>
<div data-role="collapsible" class="postRegistrationCollapsible ui-disabled" data-collapsed="true" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d">
<h3>Register / Login</h3>
<p>
<script src="js/pages/postRegistration.js"></script>
<div data-role="fieldcontain" class="hidden">
<p id="postRegistrationMessage"></p>
</div>
<a href="#registerPopupDiv" class="postRegistrationButtons" data-role="button" data-rel="popup" data-inline="true">Register</a>
<a href="#loginPopupDiv" class="postRegistrationButtons" data-role="button" data-rel="popup" data-inline="true">Login</a>
<div id="registerPopupDiv" data-role="popup" data-overlay-theme="a" class="ui-content" data-corners="true" data-shadow="true" data-position-to="window" data-transition="flow" data-theme="c">
<form id="registerFormPopup" data-ajax="false">
<div data-role="fieldcontain" data-inset="false">
<label for="first_name" class="required">First Name1:</label>
<input type="text" id="first_name" name="first_name" value="{Value}" class="required" required/>
<label for="middle_name" class="{Class}">Middle Name:</label>
<input type="text" id="middle_name" name="middle_name" value="{Value}" class="{Class}" {Required}/>
<label for="last_name" class="required">Last Name1:</label>
<input type="text" id="last_name" name="last_name" value="{Value}" class="required" required/>
<label for="email" class="{Class}">eMail:</label>
<input type="email" id="email" name="email" value="{Value}" class="{Class}" {Required}/>
<label for="login" class="{Class}">Login:</label>
<input type="text" id="login" name="login" value="{Value}" class="{Class}" {Required}/>
<label for="password" class="{Class}">Password:</label>
<input type="password" id="password" name="password" value="{Value}" class="{Class}" {Required}/>
<label for="customer_id_unique">Customer ID:</label>
<input type="text" id="customer_id_unique" name="customer_id_unique" value="{Value}" class="readonly" readonly/>
</div>
<div data-role="fieldcontain" data-inset="false">
<input type="button" name="registerPopupButton" id="registerPopupButton" value="Register"/>
</div>
</form>
</div>
<div id="loginPopupDiv" data-overlay-theme="a" data-role="popup" class="ui-content" data-corners="true" data-shadow="true" data-position-to="window" data-transition="flow" data-theme="c">
<form id="loginFormPopup" data-ajax="false">
<div data-role="fieldcontain" class="ui-hide-label">
<label for="login-username">Username:</label>
<input type="text" name="login-username" id="login-username" value="" placeholder="Username"/>
</div>
<div data-role="fieldcontain" class="ui-hide-label">
<label for="login-password">Password</label>
<input type="password" name="login-password" id="login-password" value="" placeholder="Password"/>
</div>
<div data-role="fieldcontain" data-inset="false">
<input type="button" name="loginPopupButton" id="loginPopupButton" value="Login"/>
</div>
<div data-role="fieldcontain" data-inset="false">
<a href="#" id="forgotPopupPassword" data-rel="popup">Forgot Password?</a>
</div>
</form>
</div>
</p>
</div>
<div data-role="collapsible" data-collapsed="true" class="ui-disabled" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d">
<h3>Verification</h3>
<p>
Page 5
</p>
</div>
</div>
</p>
</div>
{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>
<script src="js/pages/register.js"></script>
<form id="registerForm" data-ajax="false">
<div data-role="fieldcontain" data-inset="false">
<label for="first_name" class="required">First Name1:</label>
<input type="text" id="first_name" name="first_name" value="{Value}" class="required" required/>
<label for="middle_name" class="{Class}">Middle Name:</label>
<input type="text" id="middle_name" name="middle_name" value="{Value}" class="{Class}" {Required}/>
<label for="last_name" class="required">Last Name1:</label>
<input type="text" id="last_name" name="last_name" value="{Value}" class="required" required/>
<label for="email" class="{Class}">eMail:</label>
<input type="email" id="email" name="email" value="{Value}" class="{Class}" {Required}/>
<label for="login" class="{Class}">Login:</label>
<input type="text" id="login" name="login" value="{Value}" class="{Class}" {Required}/>
<label for="password" class="{Class}">Password:</label>
<input type="password" id="password" name="password" value="{Value}" class="{Class}" {Required}/>
<label for="customer_id_unique">Customer ID:</label>
<input type="text" id="customer_id_unique" name="customer_id_unique" value="{Value}" class="readonly" readonly/>
</div>
<div data-role="fieldcontain" data-inset="false">
<input type="button" name="register" id="register" value="Register"/>
</div>
</form>
</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>
<script src="js/pages/login.js"></script>
<form id="loginForm" data-ajax="false">
<div data-role="fieldcontain" class="ui-hide-label">
<label for="login-username">Username:</label>
<input type="text" name="login-username" id="login-username" value="" placeholder="Username"/>
</div>
<div data-role="fieldcontain" class="ui-hide-label">
<label for="login-password">Password</label>
<input type="password" name="login-password" id="login-password" value="" placeholder="Password"/>
</div>
<div data-role="fieldcontain" data-inset="false">
<input type="submit" name="loginSubmit" id="loginSubmit" value="Login"/>
</div>
<div data-role="fieldcontain" data-inset="false">
<a href="#passwordResetDiv" data-rel="popup">Forgot Password?</a>
</div>
</form>
<script src="js/pages/passwordReset.js"></script>
<div id="passwordResetDiv" data-overlay-theme="a" data-role="popup" class="ui-content" data-corners="true" data-shadow="true" data-position-to="window" data-transition="flow" data-theme="c">
<form id="passwordResetForm">
<div data-role="fieldcontain" class="ui-hide-label">
<label for="email">Email:</label>
<input type="text" name="email" id="email" value="" placeholder="Email" class="required" required/>
</div>
<div data-role="fieldcontain" data-inset="false">
<input type="submit" name="submit" id="submit" value="Reset Password"/>
</div>
</form>
</div>
</p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
整个页面的结果
Array
(
[0] => Array
(
[0] => {AppointmentStaffSelect_start}
<label for="apptCreateEditStaff" class="select required" required>With: </label>
<select name="apptCreateEditStaff" id="apptCreateEditStaff" data-native-menu="true" class="select required" required>
<option value="" data-service="" data-placeholder="true">Choose One</option>
<option data-service="{Services}" value="5490"
>Brian</option>
<option data-service="{Services}" value="4388"
>Mckenna</option>
<option data-service="{Services}" value="4387"
>Katelyn</option>
<option data-service="{Services}" value="783"
>David</option>
<option data-service="{Services}" value="4389"
>Ryan</option>
<option data-service="{Services}" value="4394"
>Ryan - c</option>
<option data-service="{Services}" value="784"
>Tami</option>
<option data-service="{Services}" value="4390"
>Jake</option>
</select>
{AppointmentStaffSelect_stop}
)
[1] => Array
(
[0] => AppointmentStaffSelect
)
)
加工精细的小部分
{AppointmentStaffSelect_start}
<label for="apptCreateEditStaff" class="select required" required>With: </label>
<select name="apptCreateEditStaff" id="apptCreateEditStaff" data-native-menu="true" class="select required" required>
<option value="" data-service="" data-placeholder="true">Choose One</option>
<option data-service="{Services}" value="5490"
>Brian</option>
<option data-service="{Services}" value="4388"
>Mckenna</option>
<option data-service="{Services}" value="4387"
>Katelyn</option>
<option data-service="{Services}" value="783"
>David</option>
<option data-service="{Services}" value="4389"
>Ryan</option>
<option data-service="{Services}" value="4394"
>Ryan - c</option>
<option data-service="{Services}" value="784"
>Tami</option>
<option data-service="{Services}" value="4390"
>Jake</option>
</select>
{AppointmentStaffSelect_stop}
<p>{Test}</p>
{AppointmentTest_start}
<p></p>
{AppointmentTest_stop}
<label for="service_id" class="select required">Service: </label>
<select name="service_id" id="service_id" data-native-menu="true" class="select required" required>
<option value="" data-placeholder="true" data-duration="0" data-cost="0.00">Choose One</option>
<option value="1174|30 Minute Gaming Pass" class="{Classes}" data-cost="3.00" data-duration="30">30 Minute Gaming Pass</option>
<option value="1176|1 Hour Gaming Pass" class="{Classes}" data-cost="5.00" data-duration="60">1 Hour Gaming Pass</option>
<option value="1177|2 Hour Gaming Pass" class="{Classes}" data-cost="7.00" data-duration="120">2 Hour Gaming Pass</option>
</select>
{AppointmentAddonService_start}
<fieldset data-role="controlgroup" id="apptCreateEditAddonServicesFieldset">
<legend>Add-on Service(s):</legend>
<input type="checkbox" name="aos-5059" id="aos-5059" data-duration="30" data-cost="10.00" class="custom {Classes}" />
<label for="aos-5059" class="{Classes}">Unlimited Snack Bar</label>
<input type="checkbox" name="aos-5060" id="aos-5060" data-duration="30" data-cost="5.00" class="custom {Classes}" />
<label for="aos-5060" class="{Classes}">Unlimited Energy Drinks</label>
<input type="checkbox" name="aos-5061" id="aos-5061" data-duration="30" data-cost="1.00" class="custom {Classes}" />
<label for="aos-5061" class="{Classes}">Candy Bar</label>
</fieldset>
{AppointmentAddonService_stop}
小部分页面的结果
Array
(
[0] => Array
(
[0] => {AppointmentStaffSelect_start}
<label for="apptCreateEditStaff" class="select required" required>With: </label>
<select name="apptCreateEditStaff" id="apptCreateEditStaff" data-native-menu="true" class="select required" required>
<option value="" data-service="" data-placeholder="true">Choose One</option>
<option data-service="{Services}" value="5490"
>Brian</option>
<option data-service="{Services}" value="4388"
>Mckenna</option>
<option data-service="{Services}" value="4387"
>Katelyn</option>
<option data-service="{Services}" value="783"
>David</option>
<option data-service="{Services}" value="4389"
>Ryan</option>
<option data-service="{Services}" value="4394"
>Ryan - c</option>
<option data-service="{Services}" value="784"
>Tami</option>
<option data-service="{Services}" value="4390"
>Jake</option>
</select>
{AppointmentStaffSelect_stop}
[1] => {AppointmentTest_start}
<p></p>
{AppointmentTest_stop}
[2] => {AppointmentAddonService_start}
<fieldset data-role="controlgroup" id="apptCreateEditAddonServicesFieldset">
<legend>Add-on Service(s):</legend>
<input type="checkbox" name="aos-5059" id="aos-5059" data-duration="30" data-cost="10.00" class="custom {Classes}" />
<label for="aos-5059" class="{Classes}">Unlimited Snack Bar</label>
<input type="checkbox" name="aos-5060" id="aos-5060" data-duration="30" data-cost="5.00" class="custom {Classes}" />
<label for="aos-5060" class="{Classes}">Unlimited Energy Drinks</label>
<input type="checkbox" name="aos-5061" id="aos-5061" data-duration="30" data-cost="1.00" class="custom {Classes}" />
<label for="aos-5061" class="{Classes}">Candy Bar</label>
</fieldset>
{AppointmentAddonService_stop}
)
[1] => Array
(
[0] => AppointmentStaffSelect
[1] => AppointmentTest
[2] => AppointmentAddonService
)
)
如何让它解析整个页面并返回所有匹配项?小部分解析得很好,但是当我尝试搜索整个页面时,它似乎只在第一个{Test}
标记之前返回结果。