我需要从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"
}
}
}
答案 0 :(得分: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);
?>