0

I have an include with a single array in it that holds 3 instructions; a "y/n" switch and a start and end date. The include meetingParams.php looks like this:

<?php
$regArray = array("n","2018-03-03","2018-03-07");
?>

I want to update those array values from time to time using a web based form. Where I get stuck is finding the correct syntax to do that. Right now I have the following:

$registration = $_POST['registration'];
$startMeeting = $_POST['startMeeting'];
$endMeeting = $_POST['endMeeting'];

$replace = array($registration, $startMeeting, $endMeeting);
$search = file_get_contents('includes/meetingParams.php');
$parsed = preg_replace('^$regArray.*$', $replace, $search);

file_put_contents("includes/meetingParams.php", $parsed);

When I run this code, the file meetingParams.php get's replaced with an empty file. What am I missing?

parboy
  • 67
  • 8

2 Answers2

1

This should work fine:

$content = '<?php
$regArray = array("'.$registration.'","'.$startMeeting.'","'.$endMeeting.'");
?>';

file_put_contents("includes/meetingParams.php", $content);
Yolo
  • 1,569
  • 1
  • 11
  • 16
0

Try this.

include_once "includes/meetingParams.php";

$registration = $_POST['registration'];
$startMeeting = $_POST['startMeeting'];
$endMeeting = $_POST['endMeeting'];

$regArray = array($registration, $startMeeting, $endMeeting);

Explanation

There is no need to use file_get_contents since you are using a PHP file you can simply include it.

What that means is that you are placing that file inside your script. Then there is no need to use RegEx to replace the array, just reassign its value.

JustCarty
  • 3,839
  • 5
  • 31
  • 51
  • I should have explained the usage better. The array values are included in the header file to control elements throughout the site. I put the 3 values in an included array so I could update the file from a web based form rather than manually edit the values. The include has no purpose on the form page itself. – parboy Apr 02 '17 at 01:05
  • In which case, you could use @Yolo answer. That will update the array without including the file on the current script. – JustCarty Apr 02 '17 at 01:08