Deleting Blank Rows in PHP File

Hey all, I created a new file which is writing ID’s numbers.
When I’m deleting the ID’s there are blank rows left in the actual file, I’m trying to remove it but it doesn’t work.

Here is how I’m trying to remove it:

$contents2 = file_get_contents($myFile);
$contents2 = preg_replace(’/\s+/’, ‘’, $contents2);
file_put_contents($myFile, $contents2);

Please advise

Try:
[php]file_put_contents($myFile, file($myFile, FILE_SKIP_EMPTY_LINES));[/php]

Or when you are reading for other purposes use file(). You can unset() array elements when you want to delete.

Hey thanks for the help, tried this but it didn’t help.
I put it as:

file_put_contents($myFile, file($myFile, FILE_SKIP_EMPTY_LINES));

Well then I don’t know if our understanding of “empty lines” is the same. Maybe post an example and explanation of data in your file. Use the Insert Code button # up top to preserve the formatting of your text in the post.

OK, this is the file code:

[php]

100002816030915

[/php]

As you can see there are 4 rows:

Three of them are empty which means only one row, I want to delete this empty rows.

This should work fine.

[php]<?php
function trimEmptyLinesFromFile($file) {
$data = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
file_put_contents($file, implode(PHP_EOL, $data));
}

trimEmptyLinesFromFile($myFile);[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service