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[], $_REQUEST` by `$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.
['https://Stackoverflow.com/questions/59116410', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10092475/']
[See Regex Demo](https://regex101.com/r/FdiQgh/1/)

`/\$_(GET|POST|REQUEST)\[[^\]]*\]/'` will match, for example, `$_GET[anything-other-than-a-right-bracket]` and all we have to do is replace it with `$var` and rewrite the file:

```
<?php
$file = file_get_contents("example.php");
$file = preg_replace('/\$_(GET|POST|REQUEST)\[[^\]]*\]/', '$var', $file);
file_put_contents("example.php", $file);

```
Is that what you want?

```
$file = file_get_contents("example.php");
$lines = explode("\n", $file);

$var = '$var';
foreach ($lines as $key => &$value) {

    if(strpos($value, '$_GET') !== false){
        $value = preg_replace('/\$_GET\[.+?\]/', $var, $value);
    }
    elseif(strpos($value, '$_POST') !== false){
        $value = preg_replace('/\$_POST\[.+?\]/', $var, $value);
    }
    elseif(strpos($value, '$_REQUEST') !== false){
        $value = preg_replace('/\$_REQUEST\[.+?\]/', $var, $value);
    }
}

```