dynamitehost
08-25-2002, 01:14 PM
I've always used mysql, but I need to do something with a flat file and I don't have a clue how to do it......
Say if I had the variable $name and I wanted to search through a file and store the line in a variable if the line began with $name, how would I go about this?
Thanks :)
chuckt101
08-25-2002, 01:23 PM
You can either read the whole file and write it back out
or (not sure if php supports) open the file in random access mode and make sure all fields are same size. That way you can search down to there and begin writing from that point until the field size (so you dont over write data)
Thats how I did it in java anyways :D
Obviously, the first option is much easier.
brianoh
08-25-2002, 03:33 PM
Say if I had the variable $name and I wanted to search through a file and store the line in a variable if the line began with $name, how would I go about this?
$name_len = strlen($name);
$lines = file($file);
foreach ($lines as $line)
{
if (substr($line, 0, $name_len) == $name)
{
$name_var = rtrim($line);
break;
}
}
I haven't tested this, but it should work. Note that it's case sensitive.
dynamitehost
08-25-2002, 04:02 PM
Thanks alot for the help, I appreciate it :D
combs
08-27-2002, 01:44 PM
Store the whole file in an array and loop through the array to search for the $name. If found store the line in variable.