Page 1 of 1
Reading A Line from Text File Modifying it and Writing
Posted: Sun Mar 14, 2010 6:08 pm
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?
Re: Reading A Line from Text File Modifying it and Writing
Posted: Sun Mar 14, 2010 6:59 pm
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.
Re: Reading A Line from Text File Modifying it and Writing
Posted: Sun Mar 14, 2010 8:46 pm
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!

IN the docs of fgets() it is mentioned that it returns the end of line character.
Re: Reading A Line from Text File Modifying it and Writing
Posted: Sun Mar 14, 2010 9:11 pm
by Christopher
Why do you think that trim() will not work?
Re: Reading A Line from Text File Modifying it and Writing
Posted: Thu Mar 18, 2010 4:41 pm
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.

Re: Reading A Line from Text File Modifying it and Writing
Posted: Thu Mar 18, 2010 6:05 pm
by Christopher
How did you "try it"? Show your code.
Re: Reading A Line from Text File Modifying it and Writing
Posted: Thu Mar 18, 2010 7:03 pm
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>";
}
?>
Re: Reading A Line from Text File Modifying it and Writing
Posted: Fri Mar 19, 2010 1:45 am
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?
Try:
Be less frustrated and more methodical.

Re: Reading A Line from Text File Modifying it and Writing
Posted: Fri Mar 19, 2010 3:40 pm
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
a line break is desired.
So, just now I did it by appending
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";
}
?>
Re: Reading A Line from Text File Modifying it and Writing
Posted: Fri Mar 19, 2010 4:03 pm
by Christopher
Excellent. Nice work.
Re: Reading A Line from Text File Modifying it and Writing
Posted: Fri Mar 19, 2010 4:30 pm
by devarishi
Christopher wrote:Excellent. Nice work.
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.