search

Home  >  Q&A  >  body text

Extract value from PHP's If-Else statement

<p>Offer.php has 200 to 300 if else statements. My team leader wants to get the value of if else statement. I need the value of $_GET and == "value". </p> <p><strong>Offer.php :</strong></p> <pre class="brush:php;toolbar:false;"><?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"; } ?></pre> <p><strong>Value.php</strong> (try it) :</p> <pre class="brush:php;toolbar:false;"><?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); ?></pre> <p><strong>Expect output :</strong></p> <pre class="brush:php;toolbar:false;">Array ( [0] => test [1] => demo [2] => try ) Array ( [0] => one [1] => two [2] => three )</pre> <p><strong>Problem: I am getting an empty array as output. </strong></p>
P粉722409996P粉722409996503 days ago515

reply all(1)I'll reply

  • P粉395056196

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

    You try this

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

    reply
    0
  • Cancelreply