使用标题(变量名称)形成.txt

时间:2016-02-14 23:11:24

标签: php

我将表单输入保存为.txt文件。我已经完成了这个部分:

twinx(ax=ax1)

由于我有很多$ _POST变量,我希望在值旁边有变量的名称。所以最佳输出看起来像这样:

$myFile = "files/".$fileid. '.txt'; $fh = fopen($myFile, 'a'); $comma_delmited_list = implode("\t", $_POST) . "\n"; fwrite($fh, $comma_delmited_list); fclose($fh); 等等。

这可能吗?

1 个答案:

答案 0 :(得分:2)

更新

$arr = array();
foreach ($_POST as $k => $v) {
    $arr[] = $k . ' = ' . $v;
}
$comma_delmited_list = implode(" ", $arr) . "\n";


逃避安全恢复:

$arr = array();
foreach ($_POST as $k => $v) {
    $arr[] = preg_replace('~\t~', ' ', preg_replace('~=~', '', $k) . ' = ' . $v);
}
$comma_delmited_list = implode("\t", $arr) . "\n";


转义安全恢复+“$ _POST数组”:

$arr = array();
foreach ($_POST as $k => $vArray) {
    if (!is_array($vArray)) {
        $vArray = array($vArray);
    }
    $i = 0;
    foreach ($vArray as $v) {
        $arr[] = preg_replace('~\t~', ' ', preg_replace('~=~', '', $k . (count($vArray) > 0 ? $i++ : '')) . ' = ' . (string)$v);
    }
}
$comma_delmited_list = implode("\t", $arr) . "\n";


这会解决您的问题吗?