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 1mo ago

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

28

—

LowBetter than 54% of packages

Maintenance42

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

470d 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.8M123](/packages/knplabs-gaufrette)[google/cloud-storage

Cloud Storage Client for PHP

34390.8M125](/packages/google-cloud-storage)[illuminate/filesystem

The Illuminate Filesystem package.

15261.6M2.6k](/packages/illuminate-filesystem)[superbalist/flysystem-google-storage

Flysystem adapter for Google Cloud Storage

26320.6M30](/packages/superbalist-flysystem-google-storage)[creocoder/yii2-flysystem

The flysystem extension for the Yii framework

2931.7M62](/packages/creocoder-yii2-flysystem)[flowjs/flow-php-server

PHP library for handling chunk uploads. Works with flow.js html5 file uploads.

2451.6M15](/packages/flowjs-flow-php-server)

PHPackages © 2026

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