TxtFile is a small library to help read and write to text file. With its easy-to-use syntax, developers can quickly create objects to handle a file, allowing them to access the data within in an efficient manner.
Install and Usage
To install it, simply require the library via composer like so:
composer require tltemplates/txtfile
Then create an object to start working on a txt file.
//access the file $file = new TxtFile('myfile.txt'); //access the file and create if it does not exist $file = new TxtFile('myfile.txt', true);
Read file
//read the whole file store result in an array $content = $file->readAllAsArray(); //read the whole file store result in a string $content = $file->readAllAsString(); //read first 10 lines $content = $file->readAsString(0, 9); $content = $file->readAsArray(0, 9); //read all and remove blank lines $content = $file->readAllWithoutBlankLines(); //read all and remove duplicated lines $content = $file->readAllWithoutDuplicatedLines();
Write to file
//write to end of file $file->append($text); $file->writeAt(100000, $text); //use any number which exceeds total lines of the file's content //write to certain line $file->writeAt(0, $text); //writeLineAt is similar to writeAt but it automatically appends line break at the end of $text $file->writeLineAt(10, $text); //empty file's content $file->emptyFile();
Did you know that having a library to make reading text files easier in PHP can give you a major advantage? With this library, you’ll have access to more efficient loading and parsing of text data, making your web development projects run smoother. Not only that, but it also has the potential to save you time and money, since there will be less manual work required.