I'm trying to replace any global variables in my example to a specific value $var as shows in the following example:
(example.php)
<?php
// before
$firstname = $_GET['firstname'];
$lastname = $_POST['lastname'];
$age = $_REQUEST['age'];
?>
As shown in the example above, I want to change any global variables $_POST, $_GET, $_REQUEST automatically in the php file to specific value $var in the php file.
Here is what I did to get each line and check if the line of code have $_POST or $_GET or $_REQUEST, then I'm trying to change any global variables in the file to specific value $var.
(test.php)
<?php
$file = file_get_contents("example.php");
$lines = explode("\n", $file);
$var = '$var';
foreach ($lines as $key => &$value) {
if(strpos($value, '$_GET') !== false){
// Replace $_GET['firstname'] and put $var
}
elseif(strpos($value, '$_POST') !== false){
// Replace $_POST['lastname'] and put $var
}
elseif(strpos($value, '$_REQUEST') !== false){
// Replace $_REQUEST['age'] and put $var
}
}
?>
The expected results to be after replace any global variables to $var is as following:
(example.php)
<?php
// The expected results to be after replace all global variables by $var
// This is how I expect the file to looks like after replace
$firstname = $var;
$lastname = $var;
$age = $var;
?>
I appreciated if anyone anyone can help me to find a suitable way to replace any $_GET, $_POST, $_REQUEST exist in the file by $var.
- note: I want to replace any
$_GET[], $_POST[], $_REQUESTby$var, $var to be stored as following:
<?php
$firstname = $var; // Just change text (remove $_GET['firstname', and put $var] in php file
$lastname = $var; // Just change text (remove $_POST['lastname', and put $var] in php file
$age = $var; // Just change text (remove $_REQUEST['age', and put $var] in php file
?>
- Note: This is how I hope the php file to be looks like.