bustacap
10-29-2004, 02:59 AM
Hi,
I was wondering if there is a way to have PHP read a file, look through its contents and find a line that matches something that I want it to and set it to a variable.
For example, my file will look like this:
line
line
line
line
line
line
password mysecretpassword
line
line
I would like it to look through all of those lines to find "password" and set the words "password mysecretpassword" to a variable.
Is this possible?
Burhan
10-29-2004, 06:39 AM
<?php
$contents = file("file.txt");
foreach($contents as $line)
{
if (strpos($line,"password") === true)
{
$password = substr($line,strpos($line," "));
}
}
?>
tiamak
10-29-2004, 07:20 AM
let me give u one more tip :)
add line like <?php exit; ?> at the begining of your config file if you want to put in into webdirectory :)
otherwise it is possible it would be readable for everyone (depending your system configuration)
bustacap
10-29-2004, 07:18 PM
Thank you very much fyrestrtr, and tiamak for the tip!
:)
bustacap
10-29-2004, 08:13 PM
One question though, what is $line? I couldn't get it to work, because $line wasn't defined -- I read up on strpos and substr and couldn't figure it out.
PS - the line of the password is always different, i.e. sometimes it's on like 8, sometimes it's on like 38... just in case that bit of information was needed.
Thanks
Burhan
10-30-2004, 02:50 AM
Originally posted by tiamak
let me give u one more tip :)
add line like <?php exit; ?> at the begining of your config file if you want to put in into webdirectory :)
otherwise it is possible it would be readable for everyone (depending your system configuration)
If you do this, then as soon as you include this config file in any php script, the script will exit automatically.
The program doesn't care if the password is on line 8, 80 or 800.
$line is the current line that is being worked on.
$contents is an array that contains all lines from the file.
Read up on file (http://www.php.net/file).