简体   繁体   中英

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.

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 {):

$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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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