Solved

How to read each line from a file in PHP but retain the extra spaces?

Posted on 2008-06-20
5
287 Views
Last Modified: 2011-10-03
How to read each line from a file in PHP but retain the extra spaces?

my file is like below, but I want to maintain the spaces when using fgets, how to do that?

Are there any other function that can maintain the spaces?

I mean all the spaces MUST BE maintained.....NOT JUST ONE SPACE, I need ALL spaces
=========================
10.112.1.101 ABE0C0542C2T
  show cdp neigh
Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge
                  S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone
 
Device ID        Local Intrfce     Holdtme    Capability  Platform  Port ID
ABE0D0444C2T     Gig 0/2            177           S       WS-C2950T-Gig 0/1
ABE0C0506C37     Gig 0/1            147          R S      WS-C3750G-Gig 1/0/24
 ABE0C0542C2T#
=========================

Open in new window

0
Comment
Question by:mawingho
5 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 21829166
$lines = file('textfile.txt');
0
 

Author Comment

by:mawingho
ID: 21829176
then how to read?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21829203
<?php
$lines = file('textfile.txt');

foreach($lines as $lineNumberMinusOne => $lineContent) {
  $actualLineNumber = $lineNumberMinusOne + 1;

  ... do something with the lines ....
}

?>
0
 
LVL 48

Expert Comment

by:hernst42
ID: 21829211
You can also use file_get_contents which loads the file as is into one variable.
0
 
LVL 29

Expert Comment

by:fosiul01
ID: 21829313
HI try this code, i have checked, it read same as original file

 <?php

$myFile = "myfile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo nl2br($theData);

?>
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question