0

Could anyone give me some ideas or solution for toggling comments in file? To read value and toggle comment/uncomment in that line where value resides.

For example, I would like to include class Model and initialize it.

In some file there are prepared includes and initializations:

//include('foo/Model.php');
//$model = new Model();

Function is needed, for those who can not understand what the question is.

How to uncomment?

6
  • 1
    Remove this //? Commented Feb 23, 2020 at 14:08
  • First read then comment. I need function that removes comments from file. Commented Feb 23, 2020 at 14:09
  • 1
    Do you intent to do it as a one-off change in your project or as a recurring task controlled through a UI? Commented Feb 23, 2020 at 14:13
  • Use a conditional? Commented Feb 23, 2020 at 14:14
  • 1
    Christoph, I would like to use it over and over again. It's about enabling, disabling apps options. Commented Feb 23, 2020 at 14:15

1 Answer 1

2

Thanks for adding more insights to your question! Actually it's a pretty interesting one.

As far as I understand you're looking for a dynamic way to comment/uncomment lines inside a file.

So let's define our parameters first:

  • We want to manipulate a specific file (we need the filename)
  • We want to toggle specific line numbers inside this file (list of line numbers)
function file_toggler(string $file, array $lineNumbers)

With this in mind I we need to read a file and split their lines into line numbers. PHP provides a handy function for this called file().

file(): Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached.

With this in mind we have everything what we need to write a function:

<?php

function file_toggler(string $file, array $lineNumbers)
{
    // normalize because file() starts with 0 and 
    // a regular user would use (1) as the first number
    $lineNumbers = array_map(function ($number) {
        return $number - 1;
    }, $lineNumbers);

    // get all lines and trim them because 
    // file() keeps newlines inside each line
    $lines = array_map('trim', file($file));

    // now we can take the line numbers and
    // check if it starts with a comment.
    foreach ($lineNumbers as $lineNumber) {
        $line = trim($lines[$lineNumber]);
        if (substr($line, 0, 2) == '//') {
            $line = substr($line, 2);
        } else {
            $line = '//' . $line;
        }

        // replace the lines with the toggled value
        $lines[$lineNumber] = $line;
    }

    // finally we write the lines to the file
    // I'm using implode() which will append a "\n" to every
    // entry inside the array and return it as a string.
    file_put_contents($file, implode(PHP_EOL, $lines));
}


toggleFileComments(__DIR__ . '/file1.php', [3, 4]);

Hope it helps :-)

Sign up to request clarification or add additional context in comments.

2 Comments

And what is wrong with simple if ($option) { include, new; }?
@u_mulder you are totally right. This is perhaps a better approach for the OPs task which he needs to solve but the answer to the question of the OP is a different one. He want's to toggle comments in a file ;)

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.