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
This will solve your problem.
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
'[^']+'
:'
[^']+
'
Now to capture the value inside the quote add a capture group:
'([^']+)'
.Capture groups can be named:
'(?<group_name>[^']+)'
. Named groups willbe added to the match with their name and their index. But accessing
$match['group_name']
is easier to read and understand.Put together:
Output:
Notes:
(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.x
allows too format and comment the pattern.