Reading A Line from Text File Modifying it and Writing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
devarishi
Forum Contributor
Posts: 101
Joined: Fri Feb 05, 2010 7:15 pm

Reading A Line from Text File Modifying it and Writing

Post by devarishi »

Hi,


I have a text file (.txt) that contains a list of values such as given below:

Code: Select all

LotusNotesAdmin
NAS_Support
Network_ETS_Support
Network_Support
Restore_Support
I want to read each line and write it to another text file by adding <option> before it and appending </option> at the end of it.

Example:

Code: Select all

 
<option>HPOVO_Support</option>
<option>Backup_Support</option>
 
 
I have designed this PHP Script:

Code: Select all

<?php
    $file = "text.txt";
    $buffer = "";
    $fp = fopen( $file, 'r' );
    while ( !feof ( $fp) ) {
    $buffer = fgets( $fp );
    echo "<option>".$buffer."</option>";
    }
?>
It outputs:

Code: Select all

<option>Backup_ETS_Support
</option><option>Backup_Support
</option><option>DC_ETS_Support
</option><option>DC_Support
The problem is the End of line Character, which is being read from the source file, because of which </option> is written on the next line.


I used stream_get_line but that would require $Length without which the script would fall in an indefinite loop!


Any ideas?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Reading A Line from Text File Modifying it and Writing

Post by Christopher »

Use the trim() function on the string read from the file. I think there may be an option for fgets() that does not get the EOL. Check the docs.
(#10850)
devarishi
Forum Contributor
Posts: 101
Joined: Fri Feb 05, 2010 7:15 pm

Re: Reading A Line from Text File Modifying it and Writing

Post by devarishi »

arborint wrote:Use the trim() function on the string read from the file. I think there may be an option for fgets() that does not get the EOL. Check the docs.


No! trim() doesn't help at all! :banghead: IN the docs of fgets() it is mentioned that it returns the end of line character.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Reading A Line from Text File Modifying it and Writing

Post by Christopher »

Why do you think that trim() will not work?
(#10850)
devarishi
Forum Contributor
Posts: 101
Joined: Fri Feb 05, 2010 7:15 pm

Re: Reading A Line from Text File Modifying it and Writing

Post by devarishi »

Christopher wrote:Why do you think that trim() will not work?

Because I tried it and it had no effect over the output. :banghead:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Reading A Line from Text File Modifying it and Writing

Post by Christopher »

How did you "try it"? Show your code.
(#10850)
devarishi
Forum Contributor
Posts: 101
Joined: Fri Feb 05, 2010 7:15 pm

Re: Reading A Line from Text File Modifying it and Writing

Post by devarishi »

Christopher wrote:How did you "try it"? Show your code.

Code: Select all

<?php
    $file = "data.txt";
    $buffer = "";
    $fp = fopen( $file, 'r' );
 
    while ( !feof ( $fp) ) {
 
[b] trim($buffer = fgets( $fp));[/b]
    echo "<option value='".$buffer."'>".$buffer."</option>";
    }
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Reading A Line from Text File Modifying it and Writing

Post by Christopher »

Before using the banging head against wall every post, check the manual (lots of good examples). Where is the return value of trim() stored in your code?

Code: Select all

trim($buffer = fgets( $fp));
Try:

Code: Select all

$buffer = trim(fgets( $fp));
Be less frustrated and more methodical. ;)
(#10850)
devarishi
Forum Contributor
Posts: 101
Joined: Fri Feb 05, 2010 7:15 pm

Re: Reading A Line from Text File Modifying it and Writing

Post by devarishi »

Well, that works but a small problem is still there which you can notice her:

Code: Select all

 
<option value='Backup_ETS_Support'>Backup_ETS_Support</option><option value='Backup_Support'>Backup_Support</option><option value='DC_ETS_Support'>DC_ETS_Support</option><option

Right after each

Code: Select all

</option>
a line break is desired.


So, just now I did it by appending

Code: Select all

"\r\n"
in the "echo" line.

Code: Select all

<?php
    $file = "data.txt";
    $buffer = "";
    $fp = fopen( $file, 'r' );
    $count = 0;
    while ( !feof ( $fp) ) {
 
    $buffer = trim(fgets( $fp));
    echo "<option value='".$buffer."'>".$buffer."</option>\r\n";
    }
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Reading A Line from Text File Modifying it and Writing

Post by Christopher »

Excellent. Nice work.
(#10850)
devarishi
Forum Contributor
Posts: 101
Joined: Fri Feb 05, 2010 7:15 pm

Re: Reading A Line from Text File Modifying it and Writing

Post by devarishi »

Christopher wrote:Excellent. Nice work.
:drunk:

I could not see an option similar to "Thread Tools" to close this post. I have some other posts still opened / under discussion and want to close them.
Post Reply