Jump to content

Reading a specific line in a textr file from a php var


auvi1

Recommended Posts

Hello everybody  :D, it’s so nice to have someone to talk to and someone to rely on. I’ve been trying to put together this reader to populate a page on my site where I can read from a specific line in a text file, this text file contains roots to snippets like feeds that I want to put inside a php include statement so that does chunks of text load in a stack order... hope I’m making any sense, I'm trying but I'm not good whit PHP and just don’t seem to make it work... here’s the problem I have:

 

1 loader file:

 

<?PHP

$myVar = file('text.txt');

count($myVar);

include ("$myVar[0]"); // <----- line I want to extract the info from the text.txt file

?>

 

2. text.txt (urls)

 

58expoliciasdetenidosenjuliosaldranfloresesquerro.html

60monrcasnoquieren comer.html

100hombrescomencarnedeunjalo.php

 

The problem I have is that if I want to include a second url using the same method, nothing works.

 

I get a message that says:

 

[function.include]: failed to open stream: Invalid argument in etc. etc.

 

But if I only put one line in the text in the text.txt file and only reference on file in the incudes, it works, if I put 5 url's in the text file it only allows me to reads de last on the list and totally ignores de first 4.

 

Also used this and same result:

 

<?PHP

$myVar = file('2010/text.txt');

$my2Var = count($myVar );

for($x = 0; $x< $my2Var ; $x++)

{

}

include ("$dic2010"."$myVar [2]");

?>

where $lines[2] is the 3th line on the text.txt file... and that’s the online it reads, if I add a 4th line in the text.txt file it will read the 4th line as long as it's the last in the list and called bye $myVar last line.

 

What I want to do is to be able to load multiple includes in different parts of my index.php

 

will you help me? Pleeeeeeease. :)

 

Link to comment
Share on other sites

Here's a function that I wrote in a class a bit back that uses the file() function to read linebyline a text file into an array, and you can set a limit as to how many lines it returns....

 

/**
         * Read a file out into an array without requiring a handle such as fopen,
         * it uses the file function.
         *
         * @param int $limit Limit the amount of lines you want to be returned in the array
         * @return array An array of elements including each individual line
         */
function lineByLine($limit=0)
{
	// Setup the array of each line, and a count of total lines
	$arrayOfLines = file($this->_workingFile);
	$lineCount = count($arrayOfLines);

	// If the user set a limit on how many lines they want returned
	if ($limit!=0)
	{
		// Loop through and unset the last element of the array
		// until it is to the desired line limit
		for ($i=$lineCount; $i>$limit; $i--)
		{
			unset($arrayOfLines[$i]);
		}
	}

	return $arrayOfLines;
}

Link to comment
Share on other sites

Ok..

 

when you do this...

?PHP
$myVar = file('text.txt');
?>

you are putting the entire file into an array. each element of the array represents one line from the file (presuming each line ends with a \n new line character). because arrays begin counting with 0 (that's a zero), you need to use zero to show the first line. So if you have 3 lines and you wanted to use each of them, you would do this...

 

?PHP
$myVar = file('text.txt');
count($myVar);
echo $myVar[0]; /* this shows the first line */
echo $myVar[1]; /* this shows the second line */?>
echo $myVar[2]; /* this shows the third line */

an easy way to see all of them would be like this...

$myVar = file('text.txt');
$numLines =count($myVar);
$i=0;
while($i<$numLines) {
  echo $myVar[$i] . "<br>"; 

By looping thru the array you could create menu lists, option lists etc etc

 

Make sense?

Link to comment
Share on other sites

OK... so I did this:

 

<?PHP

$myVar = file('ligas.txt');

count($myVar);

echo $myVar[0]; /* this shows the first line */

echo $myVar[1]; /* this shows the second line */

echo $myVar[2]; /* this shows the third line */

 

$notaPrima =  $myVar[0];

 

?>

 

<?php

include ("/dic/breves/"."$notaPrima");

?>

 

 

and I get this result in My browser:

 

 

Warning: include (/dic/breves/58expoliciasdetenidosenjuliosaldranfloresesquerro.html ) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\afn65\2010\AutoFiller.php on line 13

 

Warning: include() [function.include]: Failed opening '/dic/breves/58expoliciasdetenidosenjuliosaldranfloresesquerro.html ' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\afn65\2010\AutoFiller.php on line 13

 

The file exists, I can see that include read the variable perfectly because I can see the string, but it jus doesn't open de darn file...

  :'(

Link to comment
Share on other sites

Each array entry includes the new-line character at the end of the string, so you need to use the trim function when you include the file:

<?php
$notaPrima =  trim($myVar[0]);
include ("/dic/breves/$notaPrima");
?>

or you can apply the trim function to the whole array by using array_map

<?php
$myVar = array_map('trim',file('ligas.txt'));
?>

 

Ken

Link to comment
Share on other sites

:D You are my HERO!!! Man thank you so much, this is a total revelation, the way you do it is simply incredible... Thank You, Thank you and finally... THAN YOU!

What can I say; you’ve made my day… This is the most incredible Christmas ever.

Thank you for your knowledge, thank you for your patience, Thank you for being here.

 

Thank you kenrbnsn for the accurate answer.  And Thank you litebearer for the explanation.

 

Grate work.

 

Thank you all. :)

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.