Hi
I am wanting to use php to open a file and update data between two points.
Example:
this is other file content ----------------------------------------
this is other file content ----------------------------------------
this is other file content ----------------------------------------
this is other file content ----------------------------------------
this is other file content ----------------------------------------
this is other file content ----------------------------------------
<!-- UPDATE AREA START -->
updatable content here
<!-- UPDATE AREA END -->
Is this doable without exploding the whole files content into an array, storing, finding the update area, modify, then writing back...?
Can I just update between the comments???
Any help would be greatly appreciated.
Thanks
File editing question - Update file between 2 points?
Moderator: General Moderators
-
ianlandsman
- Forum Newbie
- Posts: 24
- Joined: Thu Dec 30, 2004 9:50 pm
- Location: New York
Yeah it's a pain but there isn't a super easy way. You have to read the entire file in. At that point you could use it as a giant string and use regex as feyd says or put it into an array, etc but either way you need to bring it all in and work with it. Best bet is to build a nice little function/class to handle it so at least when you call the code it doesn't infuriate you! Turning a bunch of lines of code into a UpdateFileComments() function makes it all better, at least mentally.
Could anyone tell me why this is not working???
I have a file with content like the example above.
----------------- all kinds of content -----------------
----------------- all kinds of content -----------------
----------------- all kinds of content -----------------
----------------- all kinds of content -----------------
<!--START-->
replace anything within these comment tags
<!--END-->
Feyd... any thoughts?
Thanks again
I have a file with content like the example above.
----------------- all kinds of content -----------------
----------------- all kinds of content -----------------
----------------- all kinds of content -----------------
----------------- all kinds of content -----------------
<!--START-->
replace anything within these comment tags
<!--END-->
Code: Select all
<?php
$thefilecontent = implode("",file("$pathtofile"));
$replaced = preg_replace('/(<!--START-->)(.*)(<!--END-->)/', "$withthis", $thefilecontent);
?>Thanks again
-
w.geoghegan
- Forum Newbie
- Posts: 7
- Joined: Mon Jan 03, 2005 7:27 pm
- Location: Bucks, UK.
Hi,
If it's a PHP file you're wanting to update I would suggest storing the data in a text file and then using this in the PHP file.
If it's a PHP file you're wanting to update I would suggest storing the data in a text file and then using this in the PHP file.
Code: Select all
<START>
<?
include("filedata.txt");
?>
</END>Try using...
Code: Select all
$replaced = preg_replace('/(<!--START-->)(.*)(<!--END-->)/s', "$withthis", $thefilecontent);