Forum Moderators: coopster
<?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.
$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);
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.
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