PHP preg_match_all, read content and exclude unwanted
<p>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):</p>

<pre><code>@ 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;
</code></pre>

<p>With below code I can explode those that ends with a semicolon (";"), but the last line should not, because it begins with a "@".</p>

<pre><code>$contents = file_get_contents($the_path);
$result = array_map('trim', explode(";", $contents));
</code></pre>

<p>Any hint to achieve this? Thanks</p>

<p><strong>UPDATE the codes:</strong></p>

<pre><code>// 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 =&gt; $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();
</code></pre>

<p><strong>UPDATE 2, works properly via DCoder:</strong></p>

<pre><code>  $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 =&gt; $match) {
         $results[$key] = $match;
        }
      }
  }


  // $results = array_combine(range(1, count($results)), array_values($results));
  return $results;
</code></pre>
