add a character to each line in a text file php

Sajeeb

i'm a newbie and i have a text file.. the contents of the text file are as follows...

text1     text4     text7
text2     text5     text8
text3     text6     text9

what i want to do is add this --->>> character to every line in the first two vertical columns of the text file using php... how can i do this.... any help would be appreciated... thanks in advance... :).. i have tried the following code though...

<?php
$fileContents = file_get_contents('mytext.txt');
$fixedFileContents = "--->>>";
file_put_contents($fixedFileContents, 'mytext.txt');
?>

the output should look something like

--->>>text1     --->>>text4     text7
--->>>text2     --->>>text5     text8
--->>>text3     --->>>text6     text9
h2ooooooo

If I understand your question correctly, you can use preg_replace and regex for this:

$fileContents = preg_replace('/^(\w+\s+)(\w+\s+)/m', '--->>>$1--->>>$2', $fileContents);

Example:

<?php
    $fileContents = <<<TEXT
text1     text4     text7
text2     text5     text8
text3     text6     text9
TEXT;
    $fileContents = preg_replace('/^(\w+\s+)(\w+\s+)/m', '--->>>$1--->>>$2', $fileContents);
    echo $fileContents;
?>

Output:

--->>>text1     --->>>text4     text7
--->>>text2     --->>>text5     text8
--->>>text3     --->>>text6     text9

DEMO

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

add a character to each line in a text file php

From Dev

Add each character from each line of a text file, to a list in Python

From Dev

How to efficiently read the first character from each line of a text file?

From Dev

Trim each line in a text file using php file functions

From Dev

Wrap each line in a text file in apostrophes and add comma to end of lines

From Dev

Add some text to each line of txt file and pass to wget

From Dev

How to sort a text file by the second index in each line using php

From Dev

Write contents of each php array on a new line in a text file

From Dev

Adding a character at beginning and at the end of each line in a text

From Dev

Adding a character at beginning and at the end of each line in a text

From Dev

how to add a character at the beginning of each line in Richtextbox

From Dev

in Notepad ++, add the text of each line at the end of the line

From Dev

How do I add a new line w/ text in a PHP file?

From Dev

Reading only first character of each line in file

From Dev

Adding file path to each line of line of text

From Dev

Comparing each line in txt file to another text file in each line

From Dev

BASH - add prefix (file path) to each line in text file using awk

From Dev

Add character to multi-line selection in Codemirror on each line of selection

From Dev

How to add a blank line after a specific character in a text file using sed?

From Dev

New line character in text file (C#)

From Dev

Determine the new line character in a text file?

From Dev

How to add text at the end of each line in unix

From Dev

getting each line text in navbar and add as id

From Dev

add "text" automatically after each line in a list

From Dev

php how to get whole line after spicific character from text file?

From Dev

How can I use grep in a loop and add new line of text each time to the same file?

From Dev

Add strings contained in a text file to end of each 4th line

From Dev

How to read character by character in text file line using C#

From Dev

Create object for each line of text file

Related Related

  1. 1

    add a character to each line in a text file php

  2. 2

    Add each character from each line of a text file, to a list in Python

  3. 3

    How to efficiently read the first character from each line of a text file?

  4. 4

    Trim each line in a text file using php file functions

  5. 5

    Wrap each line in a text file in apostrophes and add comma to end of lines

  6. 6

    Add some text to each line of txt file and pass to wget

  7. 7

    How to sort a text file by the second index in each line using php

  8. 8

    Write contents of each php array on a new line in a text file

  9. 9

    Adding a character at beginning and at the end of each line in a text

  10. 10

    Adding a character at beginning and at the end of each line in a text

  11. 11

    how to add a character at the beginning of each line in Richtextbox

  12. 12

    in Notepad ++, add the text of each line at the end of the line

  13. 13

    How do I add a new line w/ text in a PHP file?

  14. 14

    Reading only first character of each line in file

  15. 15

    Adding file path to each line of line of text

  16. 16

    Comparing each line in txt file to another text file in each line

  17. 17

    BASH - add prefix (file path) to each line in text file using awk

  18. 18

    Add character to multi-line selection in Codemirror on each line of selection

  19. 19

    How to add a blank line after a specific character in a text file using sed?

  20. 20

    New line character in text file (C#)

  21. 21

    Determine the new line character in a text file?

  22. 22

    How to add text at the end of each line in unix

  23. 23

    getting each line text in navbar and add as id

  24. 24

    add "text" automatically after each line in a list

  25. 25

    php how to get whole line after spicific character from text file?

  26. 26

    How can I use grep in a loop and add new line of text each time to the same file?

  27. 27

    Add strings contained in a text file to end of each 4th line

  28. 28

    How to read character by character in text file line using C#

  29. 29

    Create object for each line of text file

HotTag

Archive