从PHP的If-Else语句中提取值
P粉722409996
2023-08-08 14:55:14
[PHP讨论组]
<p>Offer.php有200到300个if else语句。我的团队领导希望获得if else语句的值。我需要$_GET和=="value"的值。</p>
<p><strong>Offer.php :</strong></p>
<pre class="brush:php;toolbar:false;"><?php
if (isset($_GET['test']) && $_GET['test'] == "one") {
include "hi.php";
} elseif (isset($_GET['demo']) && $_GET['demo'] == "two") {
include "hello.php";
} elseif (isset($_GET['try']) && $_GET['try'] == "three") {
include "bye.php";
} else {
include "default.php";
}
?></pre>
<p><strong>Value.php</strong> (尝试一下) :</p>
<pre class="brush:php;toolbar:false;"><?php
$code = file_get_contents("offer.php");
// Regular expression to match $_GET variables and their corresponding values
$pattern = '/isset($_GET['([^']+)'])s*&&s*$_GET['1']s*==s*"([^"]+)"/';
preg_match_all($pattern, $code, $matches);
$getValues = [];
$values = [];
foreach ($matches as $match) {
$getValues[] = $match[1];
$values[] = $match[3];
}
print_r($variables);
print_r($values);
?></pre>
<p><strong>Expect output :</strong></p>
<pre class="brush:php;toolbar:false;">Array
(
[0] => test
[1] => demo
[2] => try
)
Array
(
[0] => one
[1] => two
[2] => three
)</pre>
<p><strong>问题:我得到了空数组的输出。</strong></p>
你试试这样