从PHP的If-Else语句中提取值
P粉722409996
P粉722409996 2023-08-08 14:55:14
[PHP讨论组]

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
)

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

P粉722409996
P粉722409996

全部回复(1)
P粉395056196

你试试这样

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

?>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号