How to loop through file and delete lines starting with "#"

Discussion in 'PHP' started by bobby9101, Apr 14, 2007.

  1. #1
    I need to loop through a file and delete all lines starting with #
    I was thinking of some regex like: ^#.*\n$ will that work?
     
    bobby9101, Apr 14, 2007 IP
  2. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #2
    u will need to use preg match function .

    - read a file
    - declare an array
    - insert each line in the array
    - pregmatch function to see if the line begin with #
    - delete the line

    now implement it :p
     
    commandos, Apr 14, 2007 IP
    bobby9101 likes this.
  3. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #3
    sorry, never used pregmatch, nor writing lines into an array
    info for the n00b ;) thanks a bunch
     
    bobby9101, Apr 14, 2007 IP
  4. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #4
    preg match with example : http://ca.php.net/preg-match

    reading line and inserting them into an array :

    <?php
    if (is_readable("file.txt")) {
      $filecontent = file("file.txt");
      echo "<ul>\n";
      for ($i=0; $i < sizeof($filecontent); $i++) {
        echo "  <li>", rtrim($filecontent[$i]), "</li>\n"; // instead of echoying , apply a method , like test (if (preg match ..) xxx , else ... 
      }
      echo "</ul>\n";
    } else {
      echo "cannot read file ...\n";
    }
    ?>
    PHP:
     
    commandos, Apr 14, 2007 IP
  5. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ok, I repped you... will try to get this working.
     
    bobby9101, Apr 14, 2007 IP