Forum Moderators: coopster
$v2 = "variable";
$v2 = strtolower($v2);
$lines = file("log.txt");
$fp = fopen("log.txt", "a");
if(in_array($v2,$lines))
{
fclose($fp);
} else {
fwrite($fp, "$v2\n");
fclose($fp);
}
/*
print "<pre>";
print_r($lines);
print "</pre>";
*/
The purpose of this is to write the variable into a flat text file only if it does not exist. If it's already there, it should ignore it.
For IF statement, if I do it manually in a form of:
if (($v2 == "") ¦¦
($v2 == "variable1") ¦¦
($v2 == "variable2") ¦¦
($v2 == "variable3"))
it works like a charm.
But, if I read my file into an array where each line is an entry, it always writes, regardless if the variable is already there or not.
On a side, I found somewhere that in_array is slow, and that isset works much better. I also found that there are other ways of checking for values within the array.
I wonder which one you prefer in such cases, when you work with flat files, text only.
Thanks
This casues a logical error at the end of script
/*
print "<pre>";
print_r($lines);
print "</pre>";
*/
This will not show your new text (if it is imported to your text file), since $lines is unaffected by your if statememnt.
Try to set the $lines = file("log.txt");, after the IF
and check again
$v2 = "variable";
$v2 = strtolower($v2);
$lines = file("log.txt");
if(in_array($v2,$lines))
{
$fp = fopen("log.txt", "a");
fwrite($fp, "$v2\n");
fclose($fp);
}
and rtfm at [php.net...]
When DC speaks, Listen!
I did! And it worked! I thank you very much.
In regards of my other question, would you be fine with array_in or would rather replace it by something else that does the same job, but faster? The flat file should not be bigger than thousand lines, plus I can always empty it.
My goal is to slow the whole process the least I can.
Thanks
rtfm
Thanks for the ping anyway, it does not hurt to hear it from time to time. We should read the manuals more than we do. ;)
As a non-php programmer, three things caught my eye. Opening the file before you needed to, the redundancy you built in with if..then..else, and manually adding '\n'
That's why I went straight for the docs and found:
Note: Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used, so you still need to use rtrim() if you do not want the line ending present.
As a non-php programmer, three things caught my eye. Opening the file before you needed to, the redundancy you built in with if..then..else, and manually adding '\n'
Man, what would be if you're php programmer? I wish I'm non-programmer like you. :)
Your suggestion failed if there was no log.txt file. Not a big deal as I can create it initially and leave it there.
What I was doing with my way was to create it if it was not there. That way I can cut/paste and let go further, starting from zero (again).
But, the trade could be expensive (open the file every time), so I agree with your view and will work on the simplification of this process. Thanks for making a point.
Still, I have a question about that in_array vs. other functions. What to use and when?
Thanks
The flat file should not be bigger than thousand lines, plus I can always empty it.
Then Grab all the file contents in one string and look for "$v2\n" in that string. That will make it just 1 comparison rather than PHP needing to search the whole array.
Not sure if that would be better but sounds like it atleast ;)
[edited by: Anyango at 4:13 am (utc) on Nov. 27, 2008]