简体   繁体   English

Fwrite PHP - 如何将完整循环输出到文件?

[英]Fwrite PHP - How to output full loop to file?

I have created a script that's working great, but I want to take it to the next level and create a file for this information automatically.我已经创建了一个运行良好的脚本,但我想将它提升到一个新的水平并自动为此信息创建一个文件。 The problem is that when I add my looped elements to using fwrite, it only writes the first entry to the file.问题是,当我将循环元素添加到使用 fwrite 时,它​​只将第一个条目写入文件。

Sample code:示例代码:

$totallines=count($lines);
$i=0;
foreach($lines as $line) {
$i=$i;

$trimmed = trim($line);

$linesgetdomain = explode('/',$trimmed);
$domain = $linesgetdomain[2];
$qmark = "?";
$front301qmark = "Front Part ";
$end301qmark = " End Part";
$lastpartqmark = "Last Part $domain";

if (strpos($trimmed,$qmark) !== false) {

strstr($trimmed, '?');
echo $front301qmark;
echo substr(strstr($trimmed, '?'), strlen('?'));
echo $end301qmark;
echo "<br>";
echo $lastpartqmark;
echo "<br>";

} else {

What I tried (placing this before } else {):我试过的(把它放在 } else { 之前):

$fpqmark = fopen('test.txt', 'w');
fwrite($fpqmark, $front301qmark);
fwrite($fpqmark, substr(strstr($trimmed, '?'), strlen('?')));
fwrite($fpqmark, $end301qmark);
fwrite($fpqmark, PHP_EOL);
fwrite($fpqmark, $lastpartqmark);
fwrite($fpqmark, PHP_EOL);
fclose($fpqmark);

How can I write the entire loop to a file instead of just the first line?如何将整个循环写入文件而不仅仅是第一行? The output displays properly in normal format, but not when writing to a file.输出以正常格式正确显示,但在写入文件时不显示。

Apologizes beforehand if this is a basic question.如果这是一个基本问题,请事先道歉。 I'm new to PHP and my understandings of certain concepts are a bit vague.我是 PHP 新手,我对某些概念的理解有点模糊。 Sort of surprised I even managed to get this script to work.有点惊讶我什至设法让这个脚本工作。 (This is only a small part of it, but if I can learn how to do this, I should be able to do the rest). (这只是其中的一小部分,但如果我能学会如何做到这一点,我应该能够做到其余部分)。

instead of using write,而不是使用写,

$fpqmark = fopen('test.txt', 'w');

Use apend,使用附加,

$fpqmark = fopen('test.txt', 'a');

The it will resolve your problem.它将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM