Scanning files for a specific string

Wednesday, June 11th, 2008

Over at DevNetwork forums somebody asked for a piece of code, that scans a directory containing thousands of files for a file containg specific string. Here is the solution I came up with it: < ?php // list file extensions that you care about here $extensions = array('php','inc'); foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.')) as $Item) { ...

Grouping array contents

Tuesday, June 10th, 2008

Source data: $triples = array(); $triples[] = array('VISA','EUR',13); $triples[] = array('VISA','EUR',26); $triples[] = array('VISA','EUR',39); $triples[] = array('VISA','JPY',13); $triples[] = array('PAYPAL','JPY',13); Desired result array( array('VISA','EUR',78), array('VISA','JPY',13), array('PAYPAL','JPY',13) ) e.g. group data by both payment method and currency. My solution $triples = array(); $triples[] = array('VISA','EUR',13); $triples[] = array('VISA','EUR',26); $triples[] = array('VISA','EUR',39); $triples[] = array('VISA','JPY',13); $triples[] = array('PAYPAL','JPY',13); $result = array(); foreach($triples ...

Searching file contents with Iterators

Wednesday, June 4th, 2008

// the following code returns all lines of source.txt that contain the string Aerosmith. // Feel free to extend it for your own uses class FileGrep extends FilterIterator { private $needle; public function __construct($file, $needle) { parent::__construct(new SplFileObject($file)); ...

SimpleXMLIterator & FilterIterator combined

Tuesday, June 3rd, 2008

< ?php /* Goal: * Take a GPX (which is XML) file * Extract interesting records from it * Calculate the arithmetic average of latitudes and longitutes ...