Okay, let me explain what your code does, by writing an explanatory comment before each significant line:
// Open "d.txt" for reading, assigning the file pointer to $news
$news = fopen("d.txt", "r");
// while the end of the file associated with $news has not been reached
while (!feof($news)) {
// Read a line of up to 4096 bytes from the file into $buffer
$buffer = fgets($news, 4096);
// Print the line read
echo $buffer;
}
// Close the file
fclose($news);
Therefore, you are not doing anything to treat each tuple of country, town and monument differently from any other set of lines. You are just reading and dumping what was read onto the page. Therefore, there is no formatting done, so the format of the output is the format of the input.
A simple solution here is to observe that for every three lines read from the file, you want to print them as a single line, separated by spaces. The blank line that follows is then ignored. Hence, you can use a counter, e.g.,
$line_count = 0;
while (($buffer = fgets($news, 4096)) !== false) {
++$line_count;
// Every 4th line is blank, so only process the rest:
if ($line_count % 4 != 0) {
echo rtrim($buffer);
if ($line_count % 3 == 0) {
echo "<br />\n";
} else {
echo ' ';
}
}
}