PHPackages                             adinan-cenci/file-editor - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. adinan-cenci/file-editor

ActiveLibrary[File &amp; Storage](/categories/file-storage)

adinan-cenci/file-editor
========================

A library to read and write files.

v3.0.0(1y ago)056↓100%1MITPHPPHP &gt;=7.0

Since Jul 13Pushed 1y ago1 watchersCompare

[ Source](https://github.com/adinan-cenci/file-editor)[ Packagist](https://packagist.org/packages/adinan-cenci/file-editor)[ RSS](/packages/adinan-cenci-file-editor/feed)WikiDiscussions master Synced today

READMEChangelog (5)Dependencies (1)Versions (10)Used By (1)

File Editor
===========

[](#file-editor)

A small library to edit and read files.

This used to be part of my [json-lines](https://github.com/adinan-cenci/json-lines) library, but I decided to move to a separated repository for the sake of organization.

Instantiating
-------------

[](#instantiating)

```
use AdinanCenci\FileEditor\File;

$file = new File('my-file.txt');
```

Iterating
---------

[](#iterating)

```
foreach ($file->lines as $lineN => $line) {
    echo "$lineN: $line ";
}
```

Editing
-------

[](#editing)

### Adding a line to the end of the file

[](#adding-a-line-to-the-end-of-the-file)

```
$file->addLine('foo-bar');
```

### Adding a line to the middle of the file

[](#adding-a-line-to-the-middle-of-the-file)

```
$lineN = 5;
$line  = 'foo-bar';
$file->addLine($line, $lineN);
```

If the file has less than `$lineN` lines, the gap will be filled with empty lines.

### Adding several lines to the end of the file

[](#adding-several-lines-to-the-end-of-the-file)

```
$lines = [
    'foo: bar',
    'bar: foo',
];

$file->addLines($lines);
```

### Adding several lines in the middle of the file

[](#adding-several-lines-in-the-middle-of-the-file)

```
$lines = [
    2 => 'foo: bar',
    6 => 'bar: foo',
];

$file->addLines($lines, false);
```

### Replacing an existing line

[](#replacing-an-existing-line)

```
$lineN = 10;
$line  = 'foo-bar';
$file->setLine($lineN, $line);
```

The difference between `::addLine()` and `::setLine()` is that the latter will overwrite whatever is already present at `$lineN`.

### Replacing multiple lines

[](#replacing-multiple-lines)

```
$lines = [
    0 => 'foo: bar',
    5 => 'bar: foo',
];

$file->setLines($lines);
```

### Retrieveing a single line

[](#retrieveing-a-single-line)

```
$lineN = 10;
$line  = $file->getLine($lineN);
```

Returns `null` if the line does not exist.

### Retrieving multiple lines

[](#retrieving-multiple-lines)

```
$linesN = [0, 1, 2];
$lines  = $file->getLines($linesN);
```

### Deleting a single line

[](#deleting-a-single-line)

```
$lineN = 10;
$file->deleteLine($lineN);
```

### Deleting multiple lines

[](#deleting-multiple-lines)

```
$linesN = [0, 1, 2];
$file->deleteLines($linesN);
```

Searching
---------

[](#searching)

The library also provides a way to query the file.
Instantiate a new `Search` object, give it conditions and call the `::find()` method, it will return an array of matching lines indexed by their position in the file.

```
$search = $file->search();
$search->condition('content', 'value to compare', 'operator');
$results = $search->find();
```

### Equals operator

[](#equals-operator)

```
$search->condition('position', 10, '=');
// Will match the 11th line in the file.
```

### In operator

[](#in-operator)

```
$search->condition('content', ['Iliad', ' Odyssey'], 'IN');
// Will match lines that match either "Iliad" or "Odyssey"
// ( case insensitive ).
```

### Like operator

[](#like-operator)

```
$search->condition('content', 'foo', 'LIKE');
// Will match lines that contains the word "foo"
// e.g: "foo", "foo bar", "foofighters" etc ( case insensitive ).

$search->condition('content', ['foo', 'bar'], 'LIKE');
// It also accept arrays. This will match match
// "fool", "barrier", "barista" etc.
```

### Regex operator

[](#regex-operator)

```
$search->condition('content', '#\d{2}\/\d{2}\/\d{4}#', 'REGEX');
// Will match lines against a regex expression.
```

### Number comparison operators

[](#number-comparison-operators)

It also supports "less than", "greater than", "less than or equal", "greater than or equal" and "between".

```
$search
  ->condition('position', 2022, '')
  ->condition('position', 60, '=')
  ->condition('length', [10, 50], 'BETWEEN');
```

### Negating conditions

[](#negating-conditions)

You may also negate the conditions.

```
$search
  ->condition('content', 'Iliad', '!=') // Different to ( case insensitive ).
  ->condition('content', ['Iliad', ' Odyssey'], 'NOT IN') // case insensitive.
  ->condition('length', [10, 50], 'NOT BETWEEN')
  ->condition('content', ['foo', 'bar'], 'UNLIKE');
```

### Multiple conditions

[](#multiple-conditions)

You may add multiple conditions to a search object. By default all of the conditions must be met.

```
$search = $file->search();
$search
  ->condition('content', 'Iron Maiden', '=')
  ->condition('position', 2000, '');

$results = $search->find();
// Will match entries for Angra from before line 2010 OR
// entries for Almah from after that
```

### Order

[](#order)

You may also order the results by different properties.

```
$search = $file->search();

$search->orderBy('content', 'ASC');
// Order search results alphabetically.
$search->orderBY('length', 'DESC');
// Order results by the line's length decrescently .
```

How to install it
-----------------

[](#how-to-install-it)

Use composer.

```
composer require adinan-cenci/file-editor

```

License
-------

[](#license)

MIT

###  Health Score

29

—

LowBetter than 63% of packages

Maintenance46

Moderate activity, may be stable

Popularity9

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~140 days

Total

5

Last Release

420d ago

Major Versions

v1.1.1 → v2.0.02025-01-19

v2.0.0 → v3.0.02025-01-27

### Community

Maintainers

![](https://www.gravatar.com/avatar/3c1eb1b039e965bb307f010fb1dd41fbc79c8c9e9a65324b8f18d2bb9168097d?d=identicon)[AdinanCenci](/maintainers/AdinanCenci)

---

Top Contributors

[![adinan-cenci](https://avatars.githubusercontent.com/u/1629643?v=4)](https://github.com/adinan-cenci "adinan-cenci (26 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/adinan-cenci-file-editor/health.svg)

```
[![Health](https://phpackages.com/badges/adinan-cenci-file-editor/health.svg)](https://phpackages.com/packages/adinan-cenci-file-editor)
```

###  Alternatives

[knplabs/gaufrette

PHP library that provides a filesystem abstraction layer

2.5k39.2M118](/packages/knplabs-gaufrette)[aws/aws-sdk-php-laravel

A simple Laravel 6/7/8/9/10/11/12 service provider for including the AWS SDK for PHP.

1.7k34.8M70](/packages/aws-aws-sdk-php-laravel)[vich/uploader-bundle

Ease file uploads attached to entities

1.9k25.4M107](/packages/vich-uploader-bundle)[spatie/simple-excel

Read and write simple Excel and CSV files

1.3k9.4M40](/packages/spatie-simple-excel)[knplabs/knp-gaufrette-bundle

Allows to easily use the Gaufrette library in a Symfony project

73628.2M85](/packages/knplabs-knp-gaufrette-bundle)[google/cloud-storage

Cloud Storage Client for PHP

34388.1M95](/packages/google-cloud-storage)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
