0

I'm reading content from a text file which I'm showing on a page later, this is my code:

$lines = file("content.txt");
$i=1;
foreach($lines  as $line ){

$var["line" . $i] = $line;
$i++;
}
extract($var);

The text file includes content in this format:

bla1
bla2

and so on, there's no space behind the domains just a linebreak, now I want to concatenate the content and show it, so I do this:

$as1 = $line1.$line2;
echo $as1;

But instead of the expected result

Bla1Bla2

I get

Bla1 Bla2

What am I doing wrong ? I can assure that there's no space in the text file not behind nor infront of the content.

Veger
  • 37,240
  • 11
  • 105
  • 116
Marius Prollak
  • 368
  • 1
  • 7
  • 22
  • The browser renders newlines (`\n`) as spaces. Look into the source and you see it's still in a new line. – dan-lee Jan 08 '13 at 11:53
  • for more explanation see the link http://stackoverflow.com/questions/588356/why-does-the-browser-renders-a-newline-as-space – shail85 Jan 08 '13 at 12:30

3 Answers3

1

There's no space; but unless you tell the file() function otherwise, there is a line feed at the end of each line

$lines = file("content.txt", FILE_IGNORE_NEW_LINES);

A browser will render a line feed as a space, unless inside a or block

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

use trim for this situation

$as1 = trim($line1).trim($line2);
echo $as1;
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0

You could try trimming your input...

$lines = file("content.txt");
$i=1;
foreach($lines  as $line ){

$var["line" . $i] = trim($line);
$i++;
}
extract($var);
geedubb
  • 4,048
  • 4
  • 27
  • 38