Search code examples
phpphing

How to append text to a specific place in a file using Phing


I am trying to append text to a specific place in a file using Phing.

Using Phing's default <append> places text at the end of a file, I have also tried using <filterchain> but it does not support the <append> parameter.

Example:

I need to append the text Hello World between line2 and line3.

//config.php

line1

line2

line3

Code which appends text after line3

<append destFile="\config.php" 
        text="${line.separator} Hello World; ${line.separator}" />

Is there a better built in solution to accomplish this besides removing line2 using replace and re-adding it as line2 ${line.separator} Hello World ?

ps. I cannot use a placeholder or token since the file is downloaded from a remote source and cannot be changed.


Solution

  • The only solution I came up with was to replace the existing text before where you want to include data and then just add it back.

    For example:

     <reflexive file="\config.php">
        <filterchain>
           <replaceregexp>
            <regexp pattern="line2" replace="line2 ${line.separator} Hello World" />
           </replaceregexp>
        </filterchain>
     </reflexive>