Skip to content

Instantly share code, notes, and snippets.

@sutlxwhx
Last active August 24, 2018 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sutlxwhx/1870275d32199352965058c9eedbcced to your computer and use it in GitHub Desktop.
Save sutlxwhx/1870275d32199352965058c9eedbcced to your computer and use it in GitHub Desktop.
Delete empty lines in the file and save it
<?php
/**
* @author https://gist.github.com/sutlxwhx
*
* @version 0.2
*
* @license Apache License 2.0
*
*/
namespace sutlxwhx;
class Empt
{
/**
* @var string Set the property that will store the result
*/
public $result;
/**
* Empt constructor.
*
* @param $path_to_the_file Absolute path to the file that will be processed
*/
public function __construct($path_to_the_file)
{
$this->path = $path_to_the_file;
if ($this->validate($this->path) == true) {
return $this->result = $this->process($this->path);
} else {
return $this->result = "File you've mentioned doesn't exists or is not writable.";
}
}
/**
* Check the availability of the input file using ternary operators
*/
private function validate()
{
return file_exists($this->path) AND is_readable($this->path) ? true : false;
}
/**
* Specify the filtering process
* @return bool Status - success or failure
*/
private function process()
{
if(is_file($this->path)){
if(is_readable($this->path)){
if(is_writable($this->path)){
$file_data = file_get_contents($this->path);
$file_data = preg_replace("#\n\r\n#", "", $file_data);
$file_data = preg_replace("#\n\n#", PHP_EOL, $file_data);
file_put_contents($this->path, $file_data, LOCK_EX);
return true;
} else {
return false;
}
} else {
return false;
}
}
else {
return false;
}
}
}
/**
* Initialise Empt class and set the necessary parameter
* Dump the result element of an object
*/
/* $empt_worker = new Empt(__DIR__ . DIRECTORY_SEPARATOR . 'test.txt'); */
/* var_dump($empt_worker->result); */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment