Page 1 of 1

Write if there is a line, don't write if a line is already..

Posted: Sat Sep 02, 2006 4:57 pm
by cursed
Is it possible if I want to write to a file, but if the text is already there, then it wont write to it?

example:
If

Code: Select all

foobar
is in a text file, then
it wont write another same line that says

Code: Select all

foobar
If
foobar is ISNT in the text file, then
foobar would be written on the text file.

Hope this is clear enough!

Posted: Sat Sep 02, 2006 5:23 pm
by volka
You can read the whole file (either with file() or line-by-line via while/fgets) and compare each line. If a line matches skip writing the (not so new) line.

see http://de2.php.net/ref.filesystem

Posted: Sat Sep 02, 2006 10:32 pm
by cursed
is there a better way?

its hard using lines for me :(

Posted: Sat Sep 02, 2006 10:44 pm
by feyd

Posted: Sat Sep 02, 2006 10:46 pm
by Jenk

Code: Select all

if (in_array($string . "\n", file('file.txt'))) {
    echo 'The line ' . $string . ' exists in the file.';
} else {
    echo 'not found';
}