Jump to content

PHP Help with Array List


luisn

Recommended Posts

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!

 

Link to comment
Share on other sites

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;
	}
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.