1

I am able to choose a single line of a file(eg. dummy.php) and replace it with a text stored in a variable, called $currentdate but I want to choose several lines and replace them with several variables. For example:

$site_name in 2nd line,

$URL in 4th line,

$protocol in line 6,

$author in line 8,

$description in line 10

And $currentdate in line 12.

I've made this code, which replaces second line:

        $currentdate = date('d F Y');
$filename = getcwd() . "/dummy.php"; 
$date_line = 1; 
$lines = file( $filename , FILE_IGNORE_NEW_LINES ); 
$lines[$date_line] =    "$currentdate";
file_put_contents( $filename , implode( "\n", $lines ) );

I repeat the question, how to add several variables in several lines of that fine? Example variables and line numbers are given in this question. Also, I've made a code, but I don't know, whether it will work or not. I can't try adding this code to my project files, because I'm scared of being my code worse and disabling other codes, which are also stood in that page. Here is the non-tested code:

$site_name_line = 1; 
$URL_line = 3; 
$protocol_line = 5; 
$author_line = 7; 
$description_line = 9; 
$currentdate_line = 11;
$lines = file( $filename , FILE_IGNORE_NEW_LINES ); 
$lines[$site_name_line] =   "$site_name";
$lines[$URL_line] = "$URL";
$lines[$protocol_line] =    "$protocol";
$lines[$author_line] =  "$author";$lines[$description_line] =   "$description";$lines[$currentdate_line] =  "$currentdate";
file_put_contents( $filename , implode( "\n", $lines ) );
16
  • 1
    So what is the question you want to ask? Commented Oct 25, 2017 at 12:21
  • @matiit how to add multiple variables in multiple lines? Commented Oct 25, 2017 at 12:22
  • It will look very similar but it will be more flexible. Try thinking about it as: You need to write a function that takes, say, one associative array with line numbers as keys and replacements as values - then you'll need to repeat your code in a loop iterating over that argument. Commented Oct 25, 2017 at 12:25
  • 1
    spoon feeding is banned on SO Commented Oct 25, 2017 at 12:26
  • 1
    @user8683930 I am coming from different angle I think. SO should not be a free.... and someone said it in one sentence, thank you Tarun Commented Oct 25, 2017 at 12:26

2 Answers 2

1

I've found the answer myself. I didn't thought, I could. But I did it. Here is the whole PHP code:

<?php
if (isset($_POST['post'])){

// GET EMAIL
        $site_name = $_POST["site_name"];
        $URL = $_POST["URL"];
        $protocol = $_POST["protocol"];
        $author = $_POST["author"];
         $description = $_POST["description"];
        $currentdate = date('d F Y');
$filename = getcwd() . "/dummy.php"; 
$site_name_line = 1; 
$URL_line = 3; 
$protocol_line = 5; 
$author_line = 7; 
$description_line = 9; 
$currentdate_line = 11;
$lines = file( $filename , FILE_IGNORE_NEW_LINES ); 
$lines[$site_name_line] =   "$site_name";
$lines[$URL_line] = "$URL";
$lines[$protocol_line] =    "$protocol";
$lines[$author_line] =  "$author";$lines[$description_line] =   "$description";$lines[$currentdate_line] =  "$currentdate";
file_put_contents( $filename , implode( "\n", $lines ) );
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0
/**
 * Replace lines with given content.
  *
  * $substitutions array should look
  * [
  *   1 => "new first line content",
  *   4 => "new fourth line content",
  * ]
  *
  * @param string $filename
  * @param array $substitutions
  */
  function replaceLines($filename, array $substitutions) {
     $lines = file($filename);

     foreach ($lines as $lineNumber => $lineContent) {
         if (isset($substitutions[$lineNumber])) {
             $lines[$lineNumber] = $substitutions[$lineNumber];
         }
     } 

     file_put_contents($filename, $lines);
 }

This, of course, doesn't handle all possible edge cases like, for example, when you don't have an access to the file or if you give non-existing line in substitutions array.

3 Comments

I have found the answer in another way. But I still value your hardwork. I'll first accept your answer and then min. Well, Thanks a lot.
Is anybody found my question inappropriate/duplicate/not related to SO, then please just tell me to delete this question, don't down vote. Because I have only a little rep. If you down vote, I'll be unabled to ask questions anymore.
If you thing, my question is good and useful for others, then please upvote, So that it will be a good support

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.