使用echo值的值创建一个php文件

问题描述 投票:-1回答:3

我有一段这样的代码:

$classVoHeader = 'class C'.toCamelCase($tableName,true).'Vo{';
$classVoFooter = '}';
$str ='public $table_map = array(';
$propertyStr = '';
foreach($columnInfos as $column){
$str.=$br.'\''.$column['Field'].'\' => \''.toCamelCase($column['Field']).'\',';
    $propertyStr.=$br.'public $'.toCamelCase($column['Field']).';';
}
$str.=$br.');';
echo $classVoHeader.$br;
echo $str;
echo $propertyStr.$br;
echo $classVoFooter;

我想创建一个php文件,其内容完全是它所回应的内容。这不可能吗?

php file echo render
3个回答
1
投票

看看http://www.php.net/manual/en/function.ob-get-contents.php将正在打印的内容保存到缓冲区中,然后使用http://www.php.net/manual/en/function.file-put-contents.php将此字符串保存到文件中


1
投票

奇怪的问题,但是,你走了:

 $Result = '<?php ' . $classVoHeader.$br . $str . $propertyStr.$br . $classVoFooter . ' ?>';

 $file = fopen("result.php","w");
 echo fwrite($file,$Result);
 fclose($file);

1
投票

我不知道你的代码是什么,它无关紧要。根据我的回答,你将把ob_start();ob_get_clean();之间产生的所有输出保存到变量$the_output_of_code。然后将其写入文件。

<?php
    ob_start();

    // your code begins here

    // [your piece of code here whatever]

    $classVoHeader = 'class C'.toCamelCase($tableName,true).'Vo{';
    $classVoFooter = '}';
    $str ='public $table_map = array(';
    $propertyStr = '';
    foreach($columnInfos as $column){
        $str.=$br.'\''.$column['Field'].'\' => \''.toCamelCase($column['Field']).'\',';
        $propertyStr.=$br.'public $'.toCamelCase($column['Field']).';';
     }
     $str.=$br.');';
     echo $classVoHeader.$br;
     echo $str;
     echo $propertyStr.$br;
     echo $classVoFooter;

    // your code ends here

    $the_output_of_code = ob_get_clean();

    $file = fopen("the_output.php","w");
    fwrite($file,$the_output_of_code);
    fclose($file);

?>
© www.soinside.com 2019 - 2024. All rights reserved.