luisn Posted April 23, 2013 Share Posted April 23, 2013 Hi Guys, what would be the simplest way to do the following so basically i have 10 txt files i am waiting to see the content of it change from nothing inside to 1. Once it equals 1, then it should go and run the Line it was from on, and get 2 more txt files and get the contents of those lines only. so for ex: txt file 1.txt Line 1 Line 2 Line 3 : 1 Line 4 Line 5 Line 6 Txt file 2.txt Line 1 Hello Line 2 Bye Line 3 Fun Line 4 etc Line 5 Etc2 Line 6 Etc4 txt file 3.txt Line 1 Go Line 2 No Line 3 Please Line 4 Never so in the end, IF any of the lines in txt file 1 = 1, grab the line its on or that name "Line1" and get the following contents from file 2 and 3 and display = Fun Please This will of course be in an array. thank you for all the help guys! Quote Link to comment Share on other sites More sharing options...
lemmin Posted April 23, 2013 Share Posted April 23, 2013 You really should use a database for this, but this code should work: if ($f = fopen('file1.txt', 'r')) { $c=1; while ($line = fgets($f)) { if (preg_match('/^\d+/', $line)) break; $c++; } fclose($f); if (($word1 = getLine('file2.txt', $c)) && ($word2 = getLine('file3.txt', $c))) $string = $word1 . ' ' . $word2; else $string = 'File lines didn\'t match'; echo $string; } function getLine($file, $n) { if ($f = fopen($file, 'r')) { for ($i=1;$i<$n;$i++) { if (fgets($f) === false) return false; //file line not found } $line = fgets($f); fclose($f); return $line; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.