-1

I have a text file with data that looks like this:

#dacdcadcasvsa
#svsdvsd
#  
#sfcnakjncfkajnc
I want to keep this line
and this one

How can I remove all lines containing # and echo out the lines that don't so it looks like:

I want to keep this line
and this one

All I know is that I have to get_file_contents($filename). Would I have to put it in an array?

Any tips and guidance would be appreciated.

mexicanChica
  • 69
  • 1
  • 9

4 Answers4

1

Using file() and foreach()

$lines = file("a.txt");
foreach ( $lines as $line ) {
    if ( $line[0] != '#' ){
        echo $line;
    }
}

Just update the name of the file.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • You are missing a line break after outputting the lines. Also, I would prefer the `SplFileObject()` to `file()` (because it's object oriented and can handle large files). – masterfloda Apr 24 '18 at 16:54
  • @masterfloda - From docs - 'Each element of the array corresponds to a line in the file, with the newline still attached'. Although `SplFileObject()` is valid if you are unsure how large your files are going to be. – Nigel Ren Apr 24 '18 at 17:04
0

You can replace all the comment lines with empty strings before you output.

<div style="white-space: pre-line;">
    <?= preg_replace('/^#.*\n/m', '', file_get_contents($filename)) ?>
</div>
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0

You're thinking along the right lines; although the PHP method (function) you need is actually file_get_contents(), not get_file_contents() (as per your question).

Let's break it down:

  • We need a way of separating out our data into sortable chunks. As you stated, the best way to do this is using an array.
  • We could do this, using the hash symbol (#) as a delimiter - but this would mean the last chunk of text is a mixture of text we want to remove, and text we want to keep. Instead, we'll be using line breaks as our delimiter.
  • Once the data has been separated, we can work on removing those lines that begin with a hash symbol.

Our code will look something like this:

<?php
    // Get the file contents
    $fileContents = file_get_contents('my_file.txt'); // This could be any file extension

    // Split the file by new lines
    $contentsArr = preg_split('/\r\n|\r|\n/', $fileContents);

    // Function for removing items from an array - https://stackoverflow.com/questions/9993168/remove-item-from-array-if-item-value-contains-searched-string-character?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
    function myFilter($string) {
        return strpos($string, '?') === false;
    }

    // Remove comment items from array
    $newFileContents = array_filter($fileContents, 'myFilter');

    // Concatenate and echo out the result
    echo implode(",\n",$newFileContents);
Alex Mulchinock
  • 2,119
  • 1
  • 18
  • 25
0

An alternate because I was bored:

foreach(preg_grep('/^#/', file($filename), PREG_GREP_INVERT) as $line) {
    echo $line;
}
  • Read file lines into an array
  • Get all lines NOT starting with ^ the # character
  • Loop those lines
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87