I have a file{-code-1}
looks like the below:
$first_data = 0;
$second_data = 0;
$third_data = 0;
I have three HTML checkbox on frontend and I would like to store the value into{-code-1}
.
( The value is1
if it's checked )
Inprocess.php
, I use$_POST['data_value']
and store the value into the variables which looks like the below:
$process_first_data = isset ( $_POST['first_checkbox'] ) ? 1 : 0;
$process_second_data = isset ( $_POST['second_checkbox'] ) ? 1 : 0;
$process_third_data = isset ( $_POST['third_checkbox'] ) ? 1 : 0;
And I use this method withpreg_replace
to replace the value in{-code-1}
fromprocess.php
which looks like the below:
$find_first_data = '/\$first_data = \d;/';
$find_second_data = '/\$second_data = \d;/';
$find_third_data = '/\$third_data = \d;/';
$replace_first_data = '$first_data = ' . $process_first_data . ';';
$replace_second_data = '$second_data = ' . $process_second_data . ';';
$replace_third_data = '$third_data = ' . $process_third_data . ';';
$dir = 'path/to/file';
$file_content = file_get_contents ( $dir );
file_put_contents ( $dir, preg_replace ( $find_first_data, $replace_first_data, $file_content ) );
file_put_contents ( $dir, preg_replace ( $find_second_data, $replace_second_data, $file_content ) );
file_put_contents ( $dir, preg_replace ( $find_third_data, $replace_third_data, $file_content ) );
But now the problem is only the lastfile_put_contents
is working. For example, now only the third one is working. And if I remove the third one, there are two left, then the second one is working only. And if I remove the second and the third one, there is one left, then the first one is working only.
There is no error, sorry for the long story because I want to make it in detail. May I know why only the lastfile_put_contents
is working in this case?
It looks like all three file_put_contents() are working, but you are overwriting the same file "$dir" three times.
file_put_contents($file, $contents, $flags);
An example of $flags is FILE_APPEND which will append to the end of the file instead of overwriting.
One way to understand code better is to introduce some meaningful names for intermediate variables. So we could change this:
$dir = 'path/to/file';
$file_content = file_get_contents ( $dir );
file_put_contents ( $dir, preg_replace ( $find_first_data, $replace_first_data, $file_content ) );
file_put_contents ( $dir, preg_replace ( $find_second_data, $replace_second_data, $file_content ) );
file_put_contents ( $dir, preg_replace ( $find_third_data, $replace_third_data, $file_content ) );
To this:
$dir = 'path/to/file';
$original_file_content = file_get_contents ( $dir );
$first_new_content = preg_replace ( $find_first_data, $replace_first_data, $original_file_content );
file_put_contents ( $dir, $first_new_content );
$second_new_content = preg_replace($find_second_data, $replace_second_data, $original_file_content );
file_put_contents ( $dir, $second_new_content);
$third_new_content = preg_replace($find_third_data, $replace_third_data, $original_file_content );
file_put_contents ( $dir, $third_new_content);
Now, notice that the definitions for the three new variables don't reference each other, or re-read the file, they just look at$original_file_content
. They could actually happen in any order, before the set offile_put_contents
calls:
$dir = 'path/to/file';
$original_file_content = file_get_contents ( $dir );
$third_new_content = preg_replace($find_third_data, $replace_third_data, $original_file_content );
$second_new_content = preg_replace($find_second_data, $replace_second_data, $original_file_content );
$first_new_content = preg_replace ( $find_first_data, $replace_first_data, $original_file_content );
file_put_contents ( $dir, $first_new_content);
file_put_contents ( $dir, $second_new_content);
file_put_contents ( $dir, $third_new_content);
Sincefile_put_contents
over-writes the whole file by default, the content of the file will always be$third_new_content
.
What you probably intended is for each of the replacements to happen on the result of the previous one, then write the final result to the file at the end:
$dir = 'path/to/file';
$original_file_content = file_get_contents ( $dir );
$first_new_content = preg_replace ( $find_first_data, $replace_first_data, $original_file_content );
$second_new_content = preg_replace($find_second_data, $replace_second_data, $first_new_content );
$third_new_content = preg_replace($find_third_data, $replace_third_data, $second_new_content );
file_put_contents ( $dir, $third_new_content);
UseFILE_APPEND
option
e.g.
file_put_contents ( $dir, preg_replace ( $find_third_data, $replace_third_data, $file_content ), FILE_APPEND );
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.