如何以可读格式将php输出写入文件

时间:2014-07-24 10:53:54

标签: php json file-io

我从SOAP客户端获取结果作为响应。我试图获取此输出并在我的PHP代码中格式化它。现在我想以用户可读的格式在文件中编写此输出。我尝试了Ob_start()方法,但它没有打印输出,我尝试了其他文件编写方法,但是用html代码写的不知道为什么,它根本不可读。我正在考虑使用JSON来做到这一点,但不知道如何使用它。

代码:

if($parameter['aktion'] == 'getVehicle') 
{ 
    ob_start();
    var_dump(Login());
    $s = ob_get_clean();
     $vehicle = getVehicleValuation();
     $Serial=$vehicle['SerialEquipment'];   
     $VehicleFuel=$vehicle['VehicleFuel'];
     ob_start();

    Ob_start();
     echo "ECE_In=>". $VehicleFuel->ECE_In . "<br>";
         echo "ECE_Out=>". $VehicleFuel->ECE_Out . "<br>";
         echo "ECE_All=>". $VehicleFuel->ECE_All . "<br>";
         echo "ECE_CO2=>". $VehicleFuel->ECE_CO2 . "<br>";                  



       foreach($Serial as $key => $obj)
       {
            echo "<b>"."Serial Equipment=>" . $key . "</b>"."<br>";
            echo "Code=>". $obj->Code . "<br>";
            echo "Desc Short=>". $obj->Desc_Short . "<br>";
            echo "Desc Long=>". $obj->Desc_Long . "<br>";


            foreach($obj->Esaco as $key2 => $obj2)  
            {  
                if($obj2->EsacoMainGroupCode === null){
            // doesn't contain Esaco
            break;
                }
                else{
                  echo "<b>"."Esaco=>" . $key2 . "</b>"."<br>";                 
                echo "EsacoMainGroupCode=>". $obj2->EsacoMainGroupCode . "<br>";
                echo "EsacoMainGroupDesc=>". $obj2->EsacoMainGroupDesc . "<br>";
                echo "EsacoSubGroupCode=>".  $obj2->EsacoSubGroupCode . "<br>";
                echo "EsacoSubGroupCode=>".  $obj2->EsacoSubGroupDesc . "<br>";
                echo "EsacoSubGroupCode=>".  $obj2->EsacoGroupCode . "<br>";
                echo "EsacoSubGroupCode=>".  $obj2->EsacoGroupDesc . "<br>";  
            }       
            }           
         }  
         file_put_contents('/www/1/html/webservices/AutoDoNotificationService/schwackeNet/result1.txt', ob_get_contents());
         ob_end_flush();
}

1 个答案:

答案 0 :(得分:0)

尝试用这个代替最后两行:

$html_content = ob_get_clean(); // save the content in a variable
$file_data = str_replace('<br>', "\n", $html_content); // replace the html new lines with regular new lines
$file_data = strip_tags($file_data); // Remove all html tags
file_put_contents('/www/1/html/webservices/AutoDoNotificationService/schwackeNet/result1.txt', $file_data); // Save the file
echo $html_content; // Display the html
相关问题