Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-20
5
Medium Priority
?
293 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 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

972 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