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?

2
  • First of all, use $_POST instead of $_REQUEST, it's just... better. Commented Apr 2, 2017 at 0:52
  • You should have some validation Commented Apr 2, 2017 at 1:32

2 Answers 2

1

This should work fine:

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

file_put_contents("includes/meetingParams.php", $content);
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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.
In which case, you could use @Yolo answer. That will update the array without including the file on the current script.

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.