skip to Main Content

Offer.php have 200-300 if else statement. My team leader want to get values of if else statement. I Need values of $_GET and == "value".

Offer.php :

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

Value.php (I am trying) :

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

Expect output :

Array
(
    [0] => test
    [1] => demo
    [2] => try
)
Array
(
    [0] => one
    [1] => two
    [2] => three
)

Problem : I am getting empty array output.

2

Answers


  1. This will solve your problem.

    <?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;
    }
    
    ?>
    
    Login or Signup to reply.
  2. The equal condition contains both the name and value of the parameter. So you can completely ignore the isset().

    For matching a quoted string I typically use something like '[^']+':

    • Match start quote character '
    • Match at least one character except the quote character [^']+
    • Match end quote character '

    Now to capture the value inside the quote add a capture group: '([^']+)'.

    Capture groups can be named: '(?<group_name>[^']+)'. Named groups will
    be added to the match with their name and their index. But accessing $match['group_name'] is easier to read and understand.

    Put together:

    $pattern = '(
      # match the equal condition
      \$_GET\['(?<name>[^']+)'\]\s*==\s*"(?<value>[^"]+)"\)
      # opening curly bracket
      \s*\{\s* 
      # match include target
      include\s*"(?<target>[^"]+)"    
    )x';
    
    preg_match_all(
        $pattern,
        getPHPCode(),
        $matches,
        PREG_SET_ORDER
    );
    
    // cleanup matches
    $routes = array_map(
        static function(array $match): array {
            // filter out non-string (integer) keys
            return array_filter(
                $match,
                fn ($key) => is_string($key),
                ARRAY_FILTER_USE_KEY
            );
        },
        $matches
    ); 
    
    print_r($routes);
    
    function getPHPCode(): string {
        return <<<'PHP'
        <?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"; 
    }
    PHP;
    }
    

    Output:

    Array
    (
        [0] => Array
            (
                [name] => test
                [value] => one
                [target] => hi.php
            )
    
        [1] => Array
            (
                [name] => demo
                [value] => two
                [target] => hello.php
            )
    
        [2] => Array
            (
                [name] => try
                [value] => three
                [target] => bye.php
            )
    
    )
    

    Notes:

    • I prefer using parenthesis as delimiters: (pattern). Any non-alpha-numeric can be used as the pattern delimiter (not just /). Parenthesis are used as pairs and keep their special meaning inside the pattern.
    • The modifier x allows too format and comment the pattern.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search