...

View Full Version : How to read lines from TXT file?



toplisek
12-07-2010, 12:51 PM
I like to put tag clouds into my script like:
PHP Code:




<ul>
<li class="tag1"><a href="#">Lorem ipsum</a></li>
</ul>


How to read with PHP from defined path and its structure?
I enclose my TXT file with name tag terms. :)

goldpeng
12-07-2010, 02:44 PM
Use 'fgets' function to read text file line by line. Below is an example.

<?php
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer;
}
fclose($handle);
}
?>

toplisek
12-08-2010, 08:41 AM
You see structure? Is there mistake?

Fou-Lu
12-15-2010, 05:46 AM
Late, but spam lead me here.
Look into using fgetcsv to do this easily: http://php.ca/manual/en/function.fgetcsv.php. I don't know what all of your fields are for, but use a \t for your delimiter to chunk an array out of it. Other options include using things like *scanf family to separate it into chunks, or the strtok to tokenize it and suck the variables from the resulting strings.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum