Skip to content

Instantly share code, notes, and snippets.

@cbednarski
Created June 21, 2011 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbednarski/1038932 to your computer and use it in GitHub Desktop.
Save cbednarski/1038932 to your computer and use it in GitHub Desktop.
Filter a file using a regex pattern
<?php
// This script filters the specified file over the specified regex pattern
// Each line that matches will be included in the new file
$source = fopen(__DIR__.'/sourcefile.csv', 'r');
$target = fopen(__DIR__.'/targetfile.csv', 'w');
while($line = fgets($source))
{
if(preg_match('#^pattern$#', $line) > 0)
{
fwrite($target, $line);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment