Jump to content

Update php files dynamically ?


iPixel

Recommended Posts

Is it possible to append into an array on a seperate file and save that php file so the array doesnt rever back to its original info ?

 

example:

 

$arrayname = array("a","b","c");    ---    This is on file arrays.php

 

then have a script append additional info into the array either using include("arrays.php")

or fopen im now sure.

 

I basically want to know how i could append ( array_push ? ) additional info into that array "d","e","f" etc... and save arrays.php so the next time u view that file d,e,f etc will be there.

 

Thanks for the help.

Link to comment
Share on other sites

Yea i wish i could use a database but im stuck using a pre-made array because im just adding onto an old web application that unfortunatelly wasnt designed to use DB's. 

 

But you're saying include the old array, append new info to it and overwrite the arrays.php file ? with a newly inserted array ?

Link to comment
Share on other sites

Yeah that seems like an inefficient way to store data, but you could use preg_replace to change the text in the php file:

preg_replace("[(\$variable=array\(\".+?\"[,\".+?\"]*)\)]", "$1,\"e\",\"f\",\"g\")", file_get_contents("phpfile.php"));

 

I'm pretty sure that regex needs some cleaning up, but I hope you get the idea.

Link to comment
Share on other sites

Yea i wish i could use a database but im stuck using a pre-made array because im just adding onto an old web application that unfortunatelly wasnt designed to use DB's. 

 

But you're saying include the old array, append new info to it and overwrite the arrays.php file ? with a newly inserted array ?

 

I'm saying to have a serialized array in like, array.php and return it in the included file, then unserialize it, do some work on it, then reserialize it and write it to the file.

Link to comment
Share on other sites

try

arrays.php

<?php
$somearray = array();
?>

test.php

<?php
include('arrays.php');
print_r($somearray);
$somearray[]= rand(1,1000);
$new = var_export($somearray,true);
$text = file_get_contents('arrays.php');
$tex = preg_replace('/(?<=\$somearray = )[^;]*(?=;)/is', $new, $text);
$file = fopen('arrays.php', 'w');
fputs($file, $tex);
fclose($file);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.