Solved

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

Posted on 2008-06-20
5
284 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now