Saturday, December 07, 2013

Remove all .git files, recursively.

If your using composer or another package management system, it’s great isn’t it? But it comes with a lot of junk in the form of hidden .git repos that keep a record of every change the project has committed. You don’t want to send all that over the wire every time you deploy to staging or production. This is especially important if your using your own internal packages making it ready for anyone to come steal, accessing the full source code of your project.
Either way this shell command will remove them. I put it in a chef cookbook, you can use it however you want.
find . | grep .git | xargs rm -rf
Source: http://davidwalsh.name/delete-svn#comment-23147

Friday, December 06, 2013

Array to Comma-Separated String in PHP

PHP has some great functions for parsing and outputting comma separated value (CSV) files, but it falls short when it comes to returning that data as a string. Sure, you could map over the array with implode, but what would the result of that be if you're dealing with information that contains commas within the values themselves? Luckily, you can combine PHP's built-in functions with some well-placed stream-wrappingto fill the gaps.
First of all, the following functions are supported by PHP out-of-the-box:
  • fgetcsv - Similar to fgets() except that fgetcsv() parses the line it reads for fields in CSV format and returns an array containing the fields read.
  • fputcsv - Formats a line (passed as a fields array) as CSV and write it (terminated by a newline) to the specified file handle.
  • str_getcsv - (PHP 5.3.0 +) Parses a string input for fields in CSV format and returns an array containing the fields read.
The snippet of code below implements a new str_putcsv, which expects an array of associative arrays, and returns a CSV formatted string.
/**
 * Convert a multi-dimensional, associative array to CSV data
 * @param  array $data the array of data
 * @return string       CSV text
 */
function str_putcsv($data) {
        # Generate CSV data from array
        $fh = fopen('php://temp', 'rw'); # don't create a file, attempt
                                         # to use memory instead

        # write out the headers
        fputcsv($fh, array_keys(current($data)));

        # write out the data
        foreach ( $data as $row ) {
                fputcsv($fh, $row);
        }
        rewind($fh);
        $csv = stream_get_contents($fh);
        fclose($fh);

        return $csv;
}

Conclusion

The function above has evolved from a years worth of writing data export scripts for email distribution. There are other functions out there that do array to CSV conversion, but some introduce additional overhead for fixing the shortcomings of the CSV format. What I like is that the above code is clear and concise. By outsourcing the heavy-lifting to PHP's built-in fputcsv function, it doesn't have to involve things like str_replace to escape CSV values with commas or quotes.

Proper PHP Headers for CSV Documents (All Browsers) | Zipline Interactive

Proper PHP Headers for CSV Documents (All Browsers) | Zipline Interactive

CSV or comma separated variable files are a common way of storing and transferring data from web applications or databases. These handy files can easily be opened as a spreadsheet in common editing programs like Microsoft Excel, Apple Numbers, or Open Office Calc.
Most developers have no problem generating a CSV document but many have had problems determining the proper headers to send prior to outputting the content of a PHP variable containing CSV information. Many articles have been written on the topic but very few if any contain headers that work in all browsers in including Internet Explorer 6-9 (IE 6,7,8, and 9). Many of the header combinations available online will work in Firefox and Safari but will fail when trying to force download of a CSV in Internet Explorer.
The following CSV document header code example has been tested and works in all major browsers. When run, it will generate a CSV file using PHP, store it in a variable called CSV, then echo the contents of the CSV to the browser.
(Note: For this to work properly this needs to be the only output created by the page. Your browser will see this output as a document and not standard HTML. Any other information on the page will cause the CSV to break or will result in header errors.)
01//NAME THE FILE
02$table "test";
03 
04//BUILD CSV CONTENT
05$csv '"Column 1","Column 2"' "\n";
06 
07//BUILD CSV ROWS
08$csv .= '"Column 1 Content","Column 2 Content"' "\n";
09$csv .= '"Column 1 Content","Column 2 Content"' "\n";
10$csv .= '"Column 1 Content","Column 2 Content"' "\n";
11$csv .= '"Column 1 Content","Column 2 Content"' "\n";
12$csv .= '"Column 1 Content","Column 2 Content"' "\n";
13 
14//OUPUT HEADERS
15header("Pragma: public");
16header("Expires: 0");
17header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
18header("Cache-Control: private",false);
19header("Content-Type: application/octet-stream");
20header("Content-Disposition: attachment; filename=\"$table.csv\";" );
21header("Content-Transfer-Encoding: binary");
22 
23//OUTPUT CSV CONTENT
24echo($csv);