Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7822653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:58:56+00:00 2026-06-02T07:58:56+00:00

I have a text file I want to read, but exclude lines that contain

  • 0

I have a text file I want to read, but exclude lines that contain certain character at the start (hence “@”, or whatever one defined later):

@ I don't want this line to be read
This line should be read;
"This one" should be read, too;
'Also this one' should be read;
...etc
@ But this one should be ignored;

With below code I can explode those that ends with a semicolon (“;”), but the last line should not, because it begins with a “@”.

$contents = file_get_contents($the_path);
$result = array_map('trim', explode(";", $contents));

Any hint to achieve this? Thanks

UPDATE the codes:

// http://stackoverflow.com/questions/10257244/php-preg-match-all-read-content-and-exclude-unwanted/10257319
  $results = array();
  $matches = array();
  $the_path = '/path/to/file.txt';
  if (is_file($the_path)) {
    $contents = file_get_contents($the_path);
    if ($contents) {
      // ! array warning
      // $contents = array_map('rtrim', $contents);
      // $matches = preg_grep('#^@#', $contents, PREG_GREP_INVERT);
      $matches = preg_split("/[\r\n]/", preg_replace("/@.*?[\r\n]/", "", $contents), NULL, PREG_SPLIT_NO_EMPTY);

      if ($matches) {
        foreach ($matches as $key => $val) {
          $results[$key] = $val;
        }
      }
    }
  }
  // Attempt to remove the first 0 key, and start from 1, because 0|value0 is considered NULL
  $results = array_combine(range(1, count($results)), array_values($results));

  return !empty($results) ? $results : array();

UPDATE 2, works properly via DCoder:

  $matches = array();
  if ($contents = file($the_path)) {
      $contents = array_map('rtrim', $contents);
      $keyword = '@';
      // Still output @line
      // $matches = preg_grep('#^@#', $contents, PREG_GREP_INVERT); 
      // Ok, thanks to http://php.net/manual/de/function.preg-grep.php#85503
      $matches = preg_grep("/{$keyword}/i", $contents, PREG_GREP_INVERT);         

      // $matches = preg_split("/[\r\n]/", preg_replace("/@.*?[\r\n]/", "", $contents), NULL, PREG_SPLIT_NO_EMPTY);
      // dsm($matches);
      if ($matches) {
        foreach ($matches as $key => $match) {
         $results[$key] = $match;
        }
      }
  }


  // $results = array_combine(range(1, count($results)), array_values($results));
  return $results;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:58:59+00:00Added an answer on June 2, 2026 at 7:58 am
    // get the contents of the file as an array of lines
    $contents = file($the_path);
    if($contents === false) {
        throw new Exception("Failed to open file {$the_path}");
    }
    // drop ending newlines
    $contents = array_map('rtrim', $contents);
    
    // find all lines except those starting with @
    $matched = preg_grep('#^@#', $contents, PREG_GREP_INVERT);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file. I want read that file. But In that if
I have a text file that contains two lines of numbers, all I want
I have a text file that I want to read line by line and
I want to have a text template file that would contain variables. the file
I have a .txt file having some text. I want to read this file
I have a text file that I want to edit by rewriting it to
I have a text file that I want to edit using Java. It has
I have a text file that I want to send over the network, this
I have a text file that's appended to over time and periodically I want
I have a text file in my res/raw directory. I want to read the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.