Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open file, replace string save in different Directory

Tags:

php

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?

like image 330
tmartin314 Avatar asked Dec 03 '22 04:12

tmartin314


1 Answers

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);

?>
like image 182
shamittomar Avatar answered Dec 17 '22 19:12

shamittomar