【发布时间】: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 而不是其他两个。
-
我的问题不在于
REQUESTorPOSTorGET,我想把php文件中的所有全局变量都替换成$var,,见example.php前后请 -
我知道,这就是为什么我在评论中写下我的建议,而不是答案。
-
感谢@Mark Overton,如果您知道如何在 PHP 上下文文件中自动替换它们
-
仅供参考,您为什么要这样做?
标签: php regex replace str-replace regular-language