Hi, I have the following in a text file: Line1, Line 2, Line 3, I have to display all lines in <h1> Line text </h1> format. If I can get the lines in a variable "1 by one", I can do it but I am not able to get lines in a variable "1 by one" and then echo them. How do I read lines from this text file 1 by one and then store the line in a variable for further action? Thank you jeet
Hello, Is there a way to substitute "character reading" in the code below with "line reading"? Thanks <?php $f=fopen("file.txt","r") or exit("Unable to open file!"); while (!feof($f)) { $x=fgetc($f); // The line above stores a character in $x. echo $x; } fclose($f); ?> Instead of reading a character, can I read a line? Thanks jeet
Try 'fgets'. Personally, I like to use file_get_contents and then split on "\n": $array = split("\n", file_get_contents('filename')); Then just loop through the array. rickb
You may also try this $urls="http://www.yourdomain.com/filename.ext"; $page = join("",file("$urls")); $kw = explode("\n", $page); for($i=0;$i<count($kw);$i++){ echo $kw[$i]; } Code (markup):
Exactly what I was looking for! Thanks so much. 1 question... What is this line doing? $page = join("",file("$urls")); Thank you very much for the code. best wishes jeet
Converts the array to a string. Join is an alias for implode. It takes each piece of the array, and glues them together, using the first argument as "glue."
Or you can also do something like this //open file $fp = @fopen ($some_file, "r"); if ($fp) { //for each line in file while(!feof($fp)) { //push lines into array $this_line = fgets($fp); array_push($some_array,$this_line); } //close file fclose($fp); } Code (markup):