Rumah  >  Soal Jawab  >  teks badan

Ekstrak nilai daripada penyataan If-Else PHP

<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粉722409996P粉722409996458 hari yang lalu479

membalas semua(1)saya akan balas

  • P粉395056196

    P粉3950561962023-08-09 09:33:30

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

    balas
    0
  • Batalbalas