Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

721 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