etxnreg Posted November 20, 2009 Share Posted November 20, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/182277-missing-first-space-when-echo-file/ Share on other sites More sharing options...
rajivgonsalves Posted November 20, 2009 Share Posted November 20, 2009 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/182277-missing-first-space-when-echo-file/#findComment-961833 Share on other sites More sharing options...
Psycho Posted November 20, 2009 Share Posted November 20, 2009 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/182277-missing-first-space-when-echo-file/#findComment-961940 Share on other sites More sharing options...
rarebit Posted November 20, 2009 Share Posted November 20, 2009 It's really a HTML issue, all multiple spaces (etc) will be displayed as a single space, it's the HTML way, i'd suggest something like mjdamato but expand it to include tabs (e.g. replace with 4 non breaking spaces). Quote Link to comment https://forums.phpfreaks.com/topic/182277-missing-first-space-when-echo-file/#findComment-961949 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.