从PHP的If-Else语句中提取值
P粉722409996
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;">&lt;?php if (isset($_GET['test']) &amp;&amp; $_GET['test'] == "one") { include "hi.php"; } elseif (isset($_GET['demo']) &amp;&amp; $_GET['demo'] == "two") { include "hello.php"; } elseif (isset($_GET['try']) &amp;&amp; $_GET['try'] == "three") { include "bye.php"; } else { include "default.php"; } ?&gt;</pre> <p><strong>Value.php</strong> (尝试一下) :</p> <pre class="brush:php;toolbar:false;">&lt;?php $code = file_get_contents("offer.php"); // Regular expression to match $_GET variables and their corresponding values $pattern = '/isset($_GET['([^']+)'])s*&amp;&amp;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); ?&gt;</pre> <p><strong>Expect output :</strong></p> <pre class="brush:php;toolbar:false;">Array ( [0] =&gt; test [1] =&gt; demo [2] =&gt; try ) Array ( [0] =&gt; one [1] =&gt; two [2] =&gt; three )</pre> <p><strong>问题:我得到了空数组的输出。</strong></p>
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
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

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