【问题标题】:How to replace all global variables by another variable?如何用另一个变量替换所有全局变量?
【发布时间】:2020-03-25 17:41:39
【问题描述】:

我正在尝试将示例中的所有全局变量替换为特定值 $var,如下例所示:

(example.php)

<?php 
// before
$firstname = $_GET['firstname'];
$lastname = $_POST['lastname'];
$age = $_REQUEST['age'];
?>

如上例所示,我想将php文件中的任何全局变量$_POST, $_GET, $_REQUEST自动更改为php文件中的特定值$var

这是我获取每一行并检查代码行是否有$_POST$_GET$_REQUEST所做的,然后我试图将文件中的任何全局变量更改为特定值@987654328 @。

(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
}

}
?>

将任何全局变量替换为$var后的预期结果如下:

(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;

?>

如果有人能帮我找到合适的方法,用$var 替换文件中存在的任何$_GET, $_POST, $_REQUEST,我将不胜感激。

  • 注意:我想用$var替换任何​​$_GET[], $_POST[], $_REQUEST,$var要存储如下:
<?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
?>
  • 注意:这就是我希望 php 文件的样子。

【问题讨论】:

  • $_REQUEST 与执行 $_GET 或 $_POST 相同,因此您可能只使用 $_REQUEST 而不是其他两个。
  • 我的问题不在于REQUEST or POST or GET,我想把php文件中的所有全局变量都替换成$var,,见example.php前后请
  • 我知道,这就是为什么我在评论中写下我的建议,而不是答案。
  • 感谢@Mark Overton,如果您知道如何在 PHP 上下文文件中自动替换它们
  • 仅供参考,您为什么要这样做?

标签: php regex replace str-replace regular-language


【解决方案1】:

See Regex Demo

/\$_(GET|POST|REQUEST)\[[^\]]*\]/' 将匹配,例如,$_GET[anything-other-than-a-right-bracket],我们所要做的就是用$var 替换它并重写文件:

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

【讨论】:

  • 警告:file_put_contents() 至少需要 2 个参数,其中 1 个在
【解决方案2】:

这是你想要的吗?

$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);
    }
}

【讨论】:

  • 是的,完全正确。但是example.php 没有改变! @托托
  • @AbdallaWasef:只需将 $lines 数组中包含的所有行写入输出文件即可。
  • 不是我,我还没有投票给任何人.. 别人呵呵
  • 你是什么意思写入输出文件的所有行?
  • 我试过了:file_put_contents("example.php", implode("\n", $lines)); 但它没有改变文件中的任何内容
【解决方案3】:

解决方案:

以下代码会将$_REQUEST['thisvar'] 转换为$thisvar,以及您设置的任何其他$_GET/$_POST 变量。

如 cmets 中所述,$_REQUEST 涵盖 $_GET$_POST

foreach($_REQUEST as $key => $value) $$key = $value;

如果我修改您的示例:

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

foreach($lines as $key => $value) $$key = $value;

【讨论】:

  • 请您将此代码反映到我的示例中,并从foreach循环开始演示如何将所有全局变量替换为$var
  • 我在我的问题中添加了一条注释以便更清楚,我希望example.php 文件看起来像` $firstname = $var; `,请看问题
猜你喜欢
  • 1970-01-01
  • 2018-09-22
  • 2012-10-16
  • 1970-01-01
  • 2019-11-01
  • 2011-09-09
  • 2018-01-24
  • 2014-12-06
  • 1970-01-01
相关资源
最近更新 更多