Closed Thread Icon

Topic awaiting preservation: Read specific line or value of a file with php (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12848" title="Pages that link to Topic awaiting preservation: Read specific line or value of a file with php (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Read specific line or value of a file with php <span class="small">(Page 1 of 1)</span>\

 
Synthetic
Paranoid (IV) Inmate

From: under your rug,
Insane since: Jul 2001

posted posted 08-05-2003 00:01

Ok i've got a value lets call it $potato, which is on line 50 of a php file, but that php file has has lots of other variables and includes in it. I can't modifiy the file, but I need to read that specific variables data and echo it from another php file on my server.

So if i could fread the php file and just pull that line or just pull that variable it would be great, any ideas?

Synthetic's Chess Player Page

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 08-05-2003 00:18

Synthetic: If the file is big you should fopen( you_file ) and retrieve the lines with a fgets( ... ) until you find $potato. If the file is smaller simply use the file( ) function, browse the previously returned array until you find $potato and voilà

Hope that helps.

Mathieu "POÏ" HENRI

Synthetic
Paranoid (IV) Inmate

From: under your rug,
Insane since: Jul 2001

posted posted 08-05-2003 02:19

Yeah I had a similar thought but not quite sure how it would be done. Can you provide an example on how to search the array for $potato?

Most of the time people just point to the manual, but they fail to understand that it's not written with new commers in mind at all. If I understood half the examples they give, I would be need to be asking for help to start with

[This message has been edited by Synthetic (edited 08-05-2003).]

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 08-05-2003 02:33

You should take a look at file() function...

Here's a short example that'll grab 50. line...

<?php

// Read file into an array (line by line)
$lines = file("/path/to/file/file.php");

// Output line no. 50 (note that array is zero based)
print $lines[49];

?>


Synthetic
Paranoid (IV) Inmate

From: under your rug,
Insane since: Jul 2001

posted posted 08-05-2003 02:40

Thanks much mr. maX that works fine, where would I be without you? Eh thats easy, I'd be still reading the darn manual scratching my head trying to figure out what it was trying to say

~ Walks away singing a happy song dedicated to mr. maX ~

[This message has been edited by Synthetic (edited 08-05-2003).]

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 08-05-2003 03:02

Synthetic:Here is an implementation of the 2 methods I mentionned. They both use a regex to let you to assign $potato on any line.

code:
<?

$fileName = "path/to/file/file.php";

// method #1 / theorically best suited for bigger files
unset( $potato ); // remove this line later
$fileHandle = fopen( $fileName, "r" );
while( !isset( $potato ) && !feof( $fileHandle ) )
{
$currentLine = fgets( $fileHandle, 4096 );
if( preg_match( "/\\s*\\\$potato\\s*=\\s*\\S+/", $currentLine ) )
eval( $currentLine );
}
fclose( $fileHandle );

// method #2 / theorically best suited for shorter files
unset( $potato ); // remove this line later
$fileLinesArray = file( $fileName );
while( !isset( $potato ) && isset( $fileLinesArray ) )
{
$currentLine = array_shift( $fileLinesArray );
if( preg_match( "/\\s*\\\$potato\\s*=\\s*\\S+/", $currentLine ) )
eval( $currentLine );
}
unset( $fileLinesArray );

?>

The file( ) method is faster 99.99% of the cases.
Cheers,

Mathieu "POÏ" HENRI

[This message has been edited by poi (edited 08-05-2003).]

Synthetic
Paranoid (IV) Inmate

From: under your rug,
Insane since: Jul 2001

posted posted 08-05-2003 03:10

very cool, that will be handy too, thanks poi


Synthetic's Chess Player Page

« BackwardsOnwards »

Show Forum Drop Down Menu