PHP code example of adinan-cenci / file-editor

1. Go to this page and download the library: Download adinan-cenci/file-editor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

adinan-cenci / file-editor example snippets


use AdinanCenci\FileEditor\File;

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

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

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

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

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

$file->addLines($lines);

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

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

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

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

$file->setLines($lines);

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

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

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

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

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

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

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

$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.

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

$search
  ->condition('lineNumber', 2022, '<')
  ->condition('lineNumber', 1990, '>')
  ->condition('lineNumber', 60, '<=')
  ->condition('lineNumber', 18, '>=')
  ->condition('length', [10, 50], 'BETWEEN');

$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');

$search = $file->search();
$search
  ->condition('content', 'Iron Maiden', '=')
  ->condition('lineNumber', 2000, '<');
$results = $search->find();
// Will match entries for Iron Maiden, before the line 2000.

$search = $file->search('OR');
$search
  ->condition('content', 'Blind Guardian', '=')
  ->condition('content', 'Demons & Wizards', '=');
$results = $search->find();
// Will match entries for both Blind Guardian and Demons & Wizards.

$search = $file->search('OR');

$search->andConditionGroup()
  ->condition('content', 'Angra', '=')
  ->condition('lineNumber', 2010, '<');

$search->andConditionGroup()
  ->condition('content', 'Almah', '=')
  ->condition('lineNumber', 2010, '>');

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