4

Looking for a simple way to open a source php file, replace some predefined tags, then save the file in a different directory. I am looking for a way to do it without copying the file to a tmp dir, replacing tags, then copying the file again.

Is there a way to do this in one quick pass?

2 Answers 2

18

Well, just use file_get_contents() and file_put_contents() like below and you'll not need any temp files:

<?php

//open file and get data
$data = file_get_contents("path/to/sourcefile.php");

// do tag replacements or whatever you want
$data = str_replace("<tag1>", "<tag2>", $data);

//save it back:
file_put_contents("path/to/destinationfile.php", $data);

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Very nice. Work like a charm
0

Why not copy it to a new file, and then do your replacement in the copied file? Why would you need a temporary file?

Comments

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.