2017 © Pedro Peláez
 

library text-file

Class providing methods for editing text files on a line-by-line basis.

image

dgifford/text-file

Class providing methods for editing text files on a line-by-line basis.

  • Wednesday, February 22, 2017
  • by dgifford
  • Repository
  • 0 Watchers
  • 0 Stars
  • 40 Installations
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 0 % Grown

The README.md

Text File Edit

Class for manipulating text files line by line., (*1)

Designed for editing/ creating text files where sections or lines are easily distinguished by the lines around them., (*2)

For example, configuration files like .htaccess where sections may be defined by comments or if statements., (*3)

Instantiation

The object can be initiated by providing the constructor with:, (*4)

  • A valid file path
  • A string
  • An array of strings (lines)
$text = new TextFile( 'foo.txt' );

$text = new TextFile( "line 1\nline 2\nline 3\nline 4" );

$text = new TextFile( ["line 1", "line 2", "line 3", "line 4"] );

Or, by using the load method:, (*5)

$text = new TextFile();

$text->load( 'foo.txt' );

$text->load( "line 1\nline 2\nline 3\nline 4" );

$text->load( ["line 1", "line 2", "line 3", "line 4"] );

Array Access

The class implements array access and array iterator, so lines can be accessed using array notation and a foreach loop:, (*6)

$text->load( 'foo.txt' );

// Output the first line
echo $text[0];

// Iterate over each line
foreach( $text as $index => $line )
{
    ...
}

Editing

Lines can be edited directly using array access:, (*7)

$text->load( 'foo.txt' );

// Change the first line
echo $text[0] = 'This is the first line';

// Delete the first line
unset( $text[0] );

The following methods provide more flexibility when editing., (*8)

delete

Deletes all lines: $text->delete(), (*9)

Delete all lines containing the string 'bar': $text->delete( 'bar' ), (*10)

Deleting blocks of lines can be achieved by providing two strings to the method to identify the start and end lines., (*11)

However, lines will only be deleted if the section is unique (start and end lines only occur once) and start line is before the end line., (*12)

For example, to delete the block of lines where the first line contains 'foo' and the last line contains 'bar':, (*13)

$result = $text->delete( 'bar', 'foo' );

If the block was successfully deleted, $result will be true otherwise it will be false., (*14)

Adding a third boolean parameter will make the block exclusive, i.e. not contain the start and end lines:, (*15)

$result = $text->delete( 'bar', 'foo', false ); 

replace

The parameters for the replace method are similar to those for delete, except that replacement line(s) are included., (*16)

$text->replace( 'foo', 'replace', 'bar');

This replaces the block where the first line contains 'bar' and the last line contains 'foo', with the line 'replace'., (*17)

Again, lines will only be replaced if the block is unique and start line is before the end line. It returns false if the block is invalid., (*18)

Add false as a fourth parameter to replace the block but exclude the start and end lines:, (*19)

$text->replace( 'foo', 'replace', 'bar', false );

This method also accepts an array of lines as replacements:, (*20)

$text->replace( 'foo', ['replace', 'replace', 'replace'], 'bar', false );

find

The find returns the indexes of lines containing a search string. It is used internally by methods such as delete and replace., (*21)

To find all lines containing 'foo': $result = $text->find( 'foo' ), (*22)

The result will be an array of indexes, or false if no lines found., (*23)

This method can also be used to find unique lines by adding a second false parameter:, (*24)

$result = $text->find( 'foo', false ); 

The result will be an integer, or false if no line found or more than one line is found., (*25)

prepend

This method adds a single line containing 'foo' to the start of the file: $text->prepend( 'foo' ), (*26)

Add multiple lines to the start of the file: $text->prepend( ['foo', 'foo', 'foo'] ), (*27)

append

Add a single line containing 'foo' to the end of the file: $text->append( 'foo' ), (*28)

Add multiple lines to the end of the file: $text->append( ['foo', 'foo', 'foo'] ), (*29)

add

This method adds lines before a specified line., (*30)

To add a line containing 'bar' before the line(s) containing 'foo': $text->add( 'foo', 'bar' ), (*31)

To add multiple lines before the line(s) containing 'foo': $text->add( 'foo', ['bar', 'bar', 'bar'] ), (*32)

Extracting lines

The result of extracting lines is a new TextFile object containing the extracted lines., (*33)

As with other methods, a block of lines to extract is identified by strings in the start line and end lines., (*34)

This will extract the lines between, and including, the line containing 'bar' and the line containing 'foo':, (*35)

$result = $text->extract( 'foo', 'bar' );

Adding false as a third parameter will exclude the start and end lines: $result = $text->extract( 'foo', 'bar', false), (*36)

Leaving out the second parameter will extract all lines from the first to the end of the file, including the first line., (*37)

$result = $text->extract( 'foo' );

Output

The join method returns the lines as a string with an end of line character between each line., (*38)

By default, the PHP constant PHP_EOL is used, but it can be set by: $text->setEOL( "\n" ), (*39)

To echo the contents of the file as a string: echo $text->join(), (*40)

To save the file $text->save( 'foo.txt' ), (*41)

The Versions

22/02 2017

dev-master

9999999-dev

Class providing methods for editing text files on a line-by-line basis.

  Sources   Download

GPL-3.0

The Requires

 

php configuration array access files text iterator

22/02 2017

v2.2

2.2.0.0

Class providing methods for editing text files on a line-by-line basis.

  Sources   Download

GPL-3.0

The Requires

 

php configuration array access files text iterator

22/02 2017

v2.1

2.1.0.0

Class providing methods for editing text files on a line-by-line basis.

  Sources   Download

GPL-3.0

The Requires

 

php configuration array access files text iterator

21/02 2017

v2.0

2.0.0.0

Class providing methods for editing text files on a line-by-line basis.

  Sources   Download

GPL-3.0

The Requires

 

php configuration array access files text iterator

21/02 2017

v1.6.1

1.6.1.0

Class providing methods for editing text files on a line-by-line basis.

  Sources   Download

GPL-3.0

The Requires

 

php configuration array access files text iterator

21/02 2017

v1.6

1.6.0.0

Class providing methods for editing text files on a line-by-line basis.

  Sources   Download

GPL-3.0

The Requires

 

php configuration array access files text iterator

21/02 2017

v1.5

1.5.0.0

Class providing methods for editing text files on a line-by-line basis.

  Sources   Download

GPL-3.0

The Requires

 

php configuration array access files text iterator

21/02 2017

v1.4

1.4.0.0

Class providing methods for editing text files on a line-by-line basis.

  Sources   Download

GPL-3.0

The Requires

 

php configuration array access files text iterator

05/01 2017

v1.3.2

1.3.2.0

Class providing methods for editing text files on a line-by-line basis.

  Sources   Download

GPL-3.0

The Requires

 

php configuration array access files text iterator

05/01 2017

v1.3.1

1.3.1.0

Class providing methods for editing text files on a line-by-line basis.

  Sources   Download

GPL-3.0

The Requires

 

php configuration array access files text iterator

29/11 2016

v1.3

1.3.0.0

Class providing methods for editing text files on a line-by-line basis.

  Sources   Download

GPL-3.0

The Requires

 

php configuration array access files text iterator

29/11 2016

v1.2

1.2.0.0

Class providing methods for editing text files on a line-by-line basis.

  Sources   Download

GPL-3.0

The Requires

 

php configuration array access files text iterator

07/10 2016

v1.1

1.1.0.0

Class providing methods for editing text files on a line-by-line basis.

  Sources   Download

GPL-3.0

The Requires

 

php configuration array access files text iterator

07/10 2016

v1.0

1.0.0.0

Class providing methods for editing text files on a line-by-line basis.

  Sources   Download

GPL-3.0

The Requires

 

php configuration array access files text iterator