regex 从PHP Ifelse语句获取值

wnavrhmk  于 2023-08-08  发布在  PHP
关注(0)|答案(1)|浏览(98)

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);
?>

预期输出:

Array
(
    [0] => test
    [1] => demo
    [2] => try
)
Array
(
    [0] => one
    [1] => two
    [2] => three
)

**问题:**我得到空数组输出。

bnl4lu3b

bnl4lu3b1#

这将解决你的问题。

<?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;
}

?>

字符串

相关问题