Jump to content

Recommended Posts

Hi,

I have strange behavior.

When I echo a file and the line have a space in the beginning of the line, the space is removed.

Could somebody explain why?

How should I solve it?

 

//Niklas

$filename="/tmp/display.cfg";
$file=fopen($filename,"r") or die("couldn't create file");

while (!feof($file)){
$line = fgets($file,5000);
echo "$line <br>"; }
fclose($file);

 

 

Link to comment
https://forums.phpfreaks.com/topic/182277-missing-first-space-when-echo-file/
Share on other sites

if you seeing it in the browser it will be remove (not really actually it won't be displayed see your view source), another work around is using the pre tag (preformatted text)....

 

 

$filename="/tmp/display.cfg";
$file=fopen($filename,"r") or die("couldn't create file");
print "<pre>";
while (!feof($file)){
$line = fgets($file,5000);
echo "$line <br>"; }
fclose($file);
print "</pre>";

You should also add a \n to each line you echo out to preserve the line breaks in the code. Otherwise it makes it very hard to review the output. Also, PRE may cause some unwanted results. So, the other option is to replace all the spaces with non-breaking spaces. I also decided to use file() to read the file into an array.

 

$filename="/tmp/display.cfg";
$linesAry = file($filename) or die("couldn't read file");

foreach ($linesAry as $line)
{
    echo str_replace(' ', ' ', $line) . "<br>\n";
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.