suchen

Heim  >  Fragen und Antworten  >  Hauptteil

Extrahieren Sie den Wert aus der If-Else-Anweisung von PHP

<p>Offer.php: 200 到 300 个 if else语句. <p><strong>Angebot.php :</strong></p> <pre class="brush:php;toolbar:false;"><?php if (isset($_GET['test']) && $_GET['test'] == "one") { „hi.php“ einschließen; } elseif (isset($_GET['demo']) && $_GET['demo'] == "two") { „hello.php“ einschließen; } elseif (isset($_GET['try']) && $_GET['try'] == "drei") { „bye.php“ einschließen; } anders { include „default.php“; } ?></pre> <p><strong>Value.php</strong> (尝试一下) :</p> <pre class="brush:php;toolbar:false;"><?php $code = file_get_contents("offer.php"); // Regulärer Ausdruck zum Abgleichen von $_GET-Variablen und ihren entsprechenden Werten $pattern = '/isset($_GET['([^']+)'])s*&&s*$_GET['1']s*==s*"([^"]+) "/'; preg_match_all($pattern, $code, $matches); $getValues ​​= []; $values ​​= []; foreach ($matches als $match) { $getValues[] = $match[1]; $values[] = $match[3]; } print_r($variables); print_r($values); ?></pre> <p><strong>Ausgabe erwarten:</strong></p> <pre class="brush:php;toolbar:false;">Array ( [0] => prüfen [1] => Demo [2] => versuchen ) Array ( [0] => eins [1] => zwei [2] => drei )</pre> <p><strong>问题:我得到了空数组的输出.</strong></p>
P粉722409996P粉722409996595 Tage vor582

Antworte allen(1)Ich werde antworten

  • P粉395056196

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

    你试试这样

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

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

    }

     

    ?>

    Antwort
    0
  • StornierenAntwort