Solved

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

Posted on 2008-06-20
5
289 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 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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

751 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