1

I want to do the following

I want to create .php file (executed via cronjobs) that will paste this code $files[] = 'example.php'; to other php file (paste.php) but it has to find the lastest $files[] line like regex $files[] = '(AnythingHere)'; and after this line to paste the new line. It can have random number of pages so I have no way of knowing.

<?php

    if (!isset($php_file)) {

        $files[] = 'page1.php';

        $files[] = 'page2.php';

        $files[] = 'page3.php';

        $files[] = 'page4.php';

        $file = $files[ rand(0,count($files)) ];

I hope you guys understand what I want; can anyone help me out with this one?

2
  • Why would you want to do this? Commented Aug 17, 2011 at 17:32
  • If the line of code you wish to replace is on the same line every time, it might be easier to just replace that line with the new code. However, I highly discourage against using PHP to update PHP. As others have suggested, try including the array into the target file. Commented Aug 17, 2011 at 17:54

4 Answers 4

1

if you have ONLY $file[] = '...' in paste.php, you can simply append to the file:

$line = '$file[] = "pageX.php";' . PHP_EOL;
file_put_contents('paste.php', $line, FILE_APPEND);

of you want the last "page[]" enty.

$yourNewLine = '$file[] = "pageX.php";';  // this is an example. put your "line" prm here
$filename = 'paste.php';
$lines = file($filename);
$lines = array_reverse($lines)
$found = false;
$i = 0;
while ( ! $found )
{
    if ( strpos($lines[$i], '$files[] = ' === 0) ) 
    {
        $found = true;
        array_splice($lines, $i, 0, $yourNewLine.PHP_EOL);
    }   
    $i++;   
}
$lines = array_reverse($lines);
file_put_contents($filename, $lines);
Sign up to request clarification or add additional context in comments.

2 Comments

"Parse error: syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/paste.php on line 2"
"page[]" was a type it should be your '$file[] = "pageX.php";' parameter. edited. I switched too ' with " to make real string. I didn't put much importance on that, as it is supposed to be replaced by your variable parameter.
1

Instead of doing it this way, how about instead setting your files array in a script and then include it at the top. This way you can reference the array directly and still only have to edit the file listing in only one place.

Comments

1

Quick and dirty first-fit solution:

  1. Open the file
  2. Read each line until you find one matching your regex for $files[] = ...
  3. Read more lines until you find one that doesn't match the regex
    1. Write each line read in 2 and 3 to the output file
  4. Insert your new line into the output
  5. Write the rest of the input to the output

This may not be the best way to approach the problem, drawbacks being that you have to read each line in and compare it with your regex until you find your insertion point. You'll also probably have a temporary file for output which you'll then rename to the original filename.

You'll have 2 while loops:

while (line does not match): read next line

and then

while (line does match): read next line

Someone who knows PHP better than I do might be able to come up with something a bit cleaner, but if you're just looking for something quick to get the job done, this ought to work.

Comments

1

Having this code:

$filesArray = array('page1.php','page2.php','page3.php','page4.php','page5.php',);

then getting the php file with $data = file("path/to/editable_file.php");

foreach($data as $line)
{
    if(preg_replace("/\$filesArray\s=\sarray\([\w'.,]+()\);/", "'".$newfilename."',", $line, $match))
    {
        file_put_contents(implode("\r\n", $data));
        break;
    }
}

3 Comments

will this still work If I use the array code? "$file = $files[ rand(0,count($files)) ];" if not what code can fit to do the same thing?
Warning: file_put_contents() expects at least 2 parameters, 1 given in line 8 with <?php $newfilename = "fasda"; $data = file("array.php"); foreach($data as $line) { if(preg_replace("/\$files\s=\sarray\([\w'.,]+()\);/", "'".$newfilename."',", $line, $match)) { file_put_contents(implode("\r\n", $data)); break; } }
sorry: file_put_contents('path/to/file', $data)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.