I'm using the code below to compare the last line of a log minus the time stamp against $data, ive echo'd both of them and they are exactly the same however its still writing a new log entry every time. Am i missing something here? If the lines are the same it should break, if they aren't it should write a new entry.
$data = "This is a test."
$date = date("m/j : g:i A: ");
$lines = file('log.txt');
$last_line = $lines[count($lines)-1];
$last_line = preg_replace('/[01][0-9]\/[0-3]?[0-9] : 1?[0-9]:[0-5][0-9] (A|P)M: /','',$last_line);
if (strcmp($data, $last_line) == 0) {
break;
} else {
file_put_contents('log.txt', $date.$data.PHP_EOL, FILE_APPEND);
}
}
You should use a trim on the $last_line. There is probably a newline after it that is making the strings not equal.