0

我需要从 foreach 循环(下面的代码)创建一个文本文件,但我不确定如何。到目前为止,我已经尝试使用 fwrite,但我不确定如何将这么多数据写入 txt 文件。我还尝试使用 file_put_content(),它可以工作,但它显示了所有 HTML 并且没有换行符。关于如何显示它的任何想法?

Foreach 循环

    echo "<p>" . 'custom_weapons_v3' . "</p>";
echo  "<p>" . '{' . "</p>";


foreach($arr as $key => $a){
  echo "<br>";
  $check = $a['steamid'];
  echo $check;

    echo "<br>";
      echo "{";
    echo "<br>";
      foreach($data as $key => $r){
        $check2 = $r['steam'];
        if($check == $check2){

            echo " \"{$r['wepid']}\" //{$r['wepname']}<br />";
            echo '{' . "<br />";
                    echo '"level"' . '      "' . $r['weplvl'] . '"' . "<br />";
                    echo '"quality"'  . ' ' . '"' .  $r['weprare'] . '"' . "<br />";

                if($r['attr1'] === 0 || $r['attr1'] === ''){
                    echo '';
                }else{
                    echo '"1"' . '      "' . $r['attr1'] . ' ; ' . $r['val1'] . '"' . "<br />";
                }

                if($r['attr2'] === 0 || $r['attr2'] === ''){
                    echo '';
                }else{
                    echo '"2"' . '      "' . $r['attr2'] . ' ; ' . $r['val2'] . '"' . "<br />";
                }

                if($r['attr3'] === 0 || $r['attr3'] === ''){
                    echo '';
                }else{
                    echo '"3"' . '      "' . $r['attr3'] . ' ; ' . $r['val4'] . '"' . "<br />";
                }

                if($r['attr4'] === 0 || $r['attr4'] === ''){
                    echo '';
                }else{
                    echo '"4"' . '      "' . $r['attr4'] . ' ; ' . $r['val4'] . '"' . "<br />";
                }

                if($r['attr5'] === 0 || $r['attr5'] === ''){
                    echo '';
                }else{
                    echo '"5"' . '      "' . $r['attr5'] . ' ; ' . $r['val5'] . '"' . "<br />";
                }

                if($r['attr6'] === 0 || $r['attr6'] === ''){
                    echo '';
                }else{
                    echo '"6"' . '      "' . $r['attr6'] . ' ; ' . $r['val6'] . '"' . "<br />";
                }

                if($r['attr7'] === 0 || $r['attr7'] === ''){
                    echo '';
                }else{
                    echo '"7"' . '      "' . $r['attr7'] . ' ; ' . $r['val7'] . '"' . "<br />";
                }

                if($r['attr8'] === 0 || $r['attr8'] === ''){
                    echo '';
                }else{
                    echo '"8"' . '      "' . $r['attr8'] . ' ; ' . $r['val8'] . '"' . "<br />";
                }

                if($r['attr9'] === 0 || $r['attr9'] === ''){
                    echo '';
                }else{
                    echo '"9"' . '      "' . $r['attr9'] . ' ; ' . $r['val9'] . '"' . "<br />";
                }

                if($r['attr10'] === 0 || $r['attr10'] === ''){
                    echo '';
                }else{
                    echo '"10"' . '      "' . $r['attr10'] . ' ; ' . $r['val10'] . '"' . "<br />";
                }

                if($r['attr11'] === 0 || $r['attr11'] === ''){
                    echo '';
                }else{
                    echo '"11"' . '      "' . $r['attr11'] . ' ; ' . $r['val11'] . '"' . "<br />";
                }

                if($r['attr12'] === 0 || $r['attr12'] === ''){
                    echo '';
                }else{
                    echo '"12"' . '      "' . $r['attr12'] . ' ; ' . $r['val12'] . '"' . "<br />";
                }

                if($r['attr13'] === 0 || $r['attr13'] === ''){
                    echo '';
                }else{
                    echo '"13"' . '      "' . $r['attr13'] . ' ; ' . $r['val13'] . '"' . "<br />";
                }

                if($r['attr14'] === 0 || $r['attr14'] === ''){
                    echo '';
                }else{
                    echo '"14"' . '      "' . $r['attr14'] . ' ; ' . $r['val14'] . '"' . "<br />";
                }

                if($r['attr15'] === 0 || $r['attr15'] === ''){
                    echo '';
                }else{
                    echo '"15"' . '      "' . $r['attr15'] . ' ; ' . $r['val15'] . '"' . "<br />";
                }

                if($r['attr16'] === 0 || $r['attr16'] === ''){
                    echo '';
                }else{
                    echo '"16"' . '      "' . $r['attr16'] . ' ; ' . $r['val16'] . '"' . "<br />";
                }
                echo "}";
                echo "<br>";

      }
     }
     echo "}";
     echo "<br>";
    }

  echo "<p>" . '}' . "</p>"; 

我试图将文本显示为

custom_weapons_v3

{


STEAM_0:0:1621342
{
"0" //Bat
{
"level" "0"
"quality" "0"
"1" "23 ; 21"
"2" "231 ; 231"
"3" "231 ; 0"
}
"159" //Dalokohs Bar
{
"level" "4"
"quality" "2"
"1" "22 ; 32"
"2" "12 ; 42"
}
}

}
4

1 回答 1

0

您可以使用file_put_contents(),但使用PHP_EOL代替<br />

<?php
file_put_contents('test.html', "<html>". PHP_EOL ."<head><title>Test</title></head>". PHP_EOL ."<body>". PHP_EOL ."<h1>Hello World!</h1>". PHP_EOL ."</body>". PHP_EOL ."</html>");

?>

test.html将会,

<html>
<head><title>Test</title></head>
<body>
<h1>Hello World!</h1>
</body>
</html>

您可以使用以下代码,

<?php

$data = "";

$data .= "custom_weapons_v3". PHP_EOL;
$data .= "{". PHP_EOL;


foreach($arr as $key => $a){
  $data .= PHP_EOL
  $check = $a['steamid'];
  $data .= $check;

    $data .= PHP_EOL . "{". PHP_EOL;
    foreach($data as $key => $r){
        -------------------
        -------------------
    }
}

$data .= "}" . PHP_EOL . "}";

file_put_contents('output.txt', $data);
?>
于 2013-08-14T04:31:30.980 回答