Ekstrak nilai daripada penyataan If-Else PHP
P粉722409996
P粉722409996 2023-08-08 14:55:14
0
1
478
<p>Tawaran.php有200到300个jika lain语句。我的团队领导希望获得jika lain语句的值。我需要$_GET和;和=="value。 <p><strong>Tawaran.php :</strong></p> <pre class="brush:php;toolbar:false;"><?php if (isset($_GET['test']) && $_GET['test'] == "one") { sertakan "hi.php"; } elseif (isset($_GET['demo']) && $_GET['demo'] == "dua") { sertakan "hello.php"; } elseif (isset($_GET['try']) && $_GET['try'] == "tiga") { sertakan "bye.php"; } lain { sertakan "default.php"; } ?></pra> <p><strong>Nilai.php</strong> (尝试一下):</p> <pre class="brush:php;toolbar:false;"><?php $code = file_get_contents("offer.php"); // Ungkapan biasa untuk memadankan pembolehubah $_GET dan nilai sepadannya $pattern = '/isset($_GET['([^']+)'])s*&&s*$_GET['1']s*==s*"([^"]+) "/'; preg_match_all($pattern, $code, $matches); $getValues ​​= []; $nilai = []; foreach ($match as $match) { $getValues[] = $match[1]; $values[] = $match[3]; } print_r($pembolehubah); print_r($nilai); ?></pra> <p><strong>Jangkakan output :</strong></p> <pre class="brush:php;toolbar:false;">Array ( [0] => ujian [1] => demo [2] => cubalah ) Susunan ( [0] => satu [1] => dua [2] => tiga )</pra> <p><strong>问题:我得到了空数组的输出。</strong></p>
P粉722409996
P粉722409996

membalas semua(1)
P粉395056196

Anda cuba ini

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

?>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!