Forum Moderators: coopster

Message Too Old, No Replies

pulling data from a txt into an array

         

masterosok

11:08 pm on Dec 3, 2008 (gmt 0)

10+ Year Member



I am trying to make a script that will pull the each line of txt file and use it as an array... I have this

<?php
$file = file("test.txt");
foreach($file as $line) {
echo $line."<br />\n";
}
?>

but when I echo $line it echos every line... I want to be able to assign a variable to each line individually and call to them separately... so if the txt file reads

text
text1
text2

each line has its own variable.

Thanks in advance for any help.

mooger35

12:06 am on Dec 4, 2008 (gmt 0)

10+ Year Member



try this

$file = file_get_contents('test.txt');
$file_array = explode("\n",$file);

foreach($file_array as $line) {
echo $line."<br />\n";
}

but, if all you want to do is echo each line like it appears in the txt file you can use nl2br [ca3.php.net]

echo nl2br($file);

masterosok

12:46 am on Dec 4, 2008 (gmt 0)

10+ Year Member



The text file will only contain one word per line and the purpose it to randomly pull one of the lines from the text file, but only one line, not each line...

mooger35

3:37 am on Dec 4, 2008 (gmt 0)

10+ Year Member



well this

$file = file_get_contents('test.txt');
$file_array = explode("\n",$file);

will give you an array of:

$file_array[0] = text
$file_array[1] = text1
$file_array[2] = text2

You can also use count to determine how many lines there are, and thus, how many variables.

masterosok

5:46 am on Dec 4, 2008 (gmt 0)

10+ Year Member



Thanks works great.. except for one this.... its placing a space(shows as a small box in the text file) after each word... I am not exactly sure how to prevent that...

here is what i have so far...

<?php

$list = file_get_contents('wordlist.txt');
$file_array = explode("\n",$list);

$output = $file_array[rand(0,count($file_array)-1)];

$filename = 'randomlist.txt';
$somecontent = "$output";

if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}

if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote ($somecontent) to file ($filename)";

fclose($handle);

?>

Thanks again........ I'm really new to php and not very good at debugging.

masterosok

4:08 pm on Dec 4, 2008 (gmt 0)

10+ Year Member



I change the delimiter for the explode command to ";" and added a ; to the end of each word in my word list and it works correctly now, but I would still like to figured out how to get it to work without having to add an extra character to each word in my word list... to avoid specific formatting of the list other than one word per line.

Little_G

5:06 pm on Dec 4, 2008 (gmt 0)

10+ Year Member



Hi,

The space at the end of each line might be caused by the different line endings used by different systems.
If the text file was created using Windows try using "\r\n" as a delimiter instead.

As a side note the file [php.net] function will read a file into an array automatically.

Andrew

masterosok

5:57 pm on Dec 4, 2008 (gmt 0)

10+ Year Member



Thank you very much... that fixed the issue now is just like I wanted it.