Offer.php有200到300个if else语句。我的团队领导希望获得if else语句的值。我需要$_GET和=="value"的值。
Offer.php :
<?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";
}
?>
Value.php (尝试一下) :
<?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);
?>
Expect output :
Array
(
[0] => test
[1] => demo
[2] => try
)
Array
(
[0] => one
[1] => two
[2] => three
)
问题:我得到了空数组的输出。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你试试这样
<?php $code = file_get_contents("offer.php"); $pattern_get = '/isset\(\$_GET\[\'(.*?)\'\]/'; $pattern_value = '/\$_GET\[\'(.*?)\'\]\s*==\s*"(.*?)"/'; preg_match_all($pattern_get, $code, $matches_get, PREG_SET_ORDER); preg_match_all($pattern_value, $code, $matches_value, PREG_SET_ORDER); $getValues = []; $values = []; foreach ($matches_get as $match) { $getValues[] = $match[1]; } foreach ($matches_value as $match) { $values[] = $match[2]; } print_r($getValues); print_r($values); // Creating separate URLs for each $_GET variable and value for ($i = 0; $i < count($getValues); $i++) { $url = 'example.com/?' . $getValues[$i] . '=' . $values[$i]; echo $url . '<br>' . PHP_EOL; } ?>