0

I have a text file with some URLs like:

http://1.com
http://2.com
http://3.com
http://n.com

I need to open that file and run a code for each line in this file, in other words, run the code for every URL listed. By now the code is working perfectly when I have a single URL in an array.. but if I could do this using this file full of URLs together, that would be so much faster

I was trying something like:

    $file1 = "/file.txt";
    $lines = file($file1);
    foreach($lines as $line_num => $v)
   {
    // my code
}

and running the code using the $v array.. but that's not working because all the lines are inside the $v array all together.. So, how can I run my code for every URL in the file?

Argo
  • 103
  • 2
  • 9
  • 2
    `file()` returns an array of each line in the file. It sounds like your file only has one line in it. – nickb Jan 22 '13 at 19:53
  • 2
    so you've got some wonky line-ending chars in there (e.g. `\n` whereas php is expecting `\r\n` and so never detects a "new" line).... – Marc B Jan 22 '13 at 19:53
  • How do you know that the file is all inside `$v`? Please add `var_dump($v)` and add the result to your question. – Sven Jan 22 '13 at 21:17

1 Answers1

0

It looks like your line endings are not being read into PHP, you could create your own split function like in this answer:

foreach(preg_split("/((\r?\n)|(\r\n?))/", $lines) as $line_num => $v){

} 

You could also do something like this:

$fd = fopen ($file1, "r"); 
while (!feof ($fd))  { 
   $line = fgets($fd, 4096); 
   //Do something with $line
} 
fclose ($fd); 
Community
  • 1
  • 1
fanfavorite
  • 5,128
  • 1
  • 31
  • 58