Skip to content

Instantly share code, notes, and snippets.

View cbednarski's full-sized avatar

Chris Bednarski cbednarski

View GitHub Profile
@cbednarski
cbednarski / gist:1038932
Created June 21, 2011 21:21
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))
{
@cbednarski
cbednarski / gist:1038947
Created June 21, 2011 21:27
Detach a process from an apache web request
<?php
/**
* Detaches a process from apache so it can be invoked by a webpage but run
* under CLI context. Prevents apache from waiting for the process to
* finish before displaying output. All output is piped to null.
*
* @param string $process full path to executable / binary
* @param string $arguments arguments passed to the script
*/
@cbednarski
cbednarski / gist:1038955
Created June 21, 2011 21:32
Truncates a string after n character, respecting word boundaries
<?php
/**
* Truncates $string after $length characters, without splitting words.
*
* Inspired by http://stackoverflow.com/questions/250357
*
* @param string $string
* @param integer $length
* @return string
@cbednarski
cbednarski / gist:3856190
Created October 9, 2012 02:13
Terminal colors with PHP
<?php
// Based on info from:
// - http://www.sitepoint.com/forums/showthread.php?398020-terminal-colors-in-PHP
// - http://www.dzone.com/snippets/colorized-text-php-unix
$colors = array(
'light_red' => "[1;31m",
'light_green' => "[1;32m",
'yellow' => "[1;33m",
@cbednarski
cbednarski / gist:4138965
Last active October 13, 2015 04:27
javascript code to convert AWS hour rates to monthly rates (put it in a bookmarklet)
rates = jQuery(".rate");
for(i in rates) {
rate = rates[i].innerHTML;
if(rate){
matches = rate.match(/\$([\d.]+) per hour/i);
if(matches instanceof Array) {
rates[i].innerHTML = "$"+ Math.ceil(parseFloat(matches[1])*100*(365/12)*24)/100 + " per month";
}
}
}
@cbednarski
cbednarski / gist:4449892
Last active December 10, 2015 14:48
Sublime text build system for phpunit -- runs on the current file
// Mac Lion using homebrew
{
"cmd": ["/usr/local/opt/php54/bin/php", "/usr/local/bin/phpunit", "$file"]
}
// Ubuntu 12.04 with pear-installed PHPUnit
// Ctrl + B to run phpunit
// Ctrl + Shift + B to run the script
@cbednarski
cbednarski / gist:4507950
Created January 11, 2013 04:27
Syntax-specific settings for Sublime Text 2
{
"tab_size": 4,
"translate_tabs_to_spaces": true
}
@cbednarski
cbednarski / gist:4555141
Created January 17, 2013 10:37
Sublime Text keymap, which fixes the really annoying Ctrl+Tab order behavior
[
{ "keys": ["ctrl+shift+t"], "command": "open_terminal_project_folder" },
{ "keys": ["ctrl+tab"], "command": "next_view" },
{ "keys": ["ctrl+shift+tab"], "command": "prev_view" }
]
@cbednarski
cbednarski / gist:4983212
Created February 19, 2013 04:51
Read from standard input using php
<?php
$f = fopen('php://stdin', 'r');
while ($line = fgets($f)) {
// do stuff
}
fclose($f);
@cbednarski
cbednarski / 404.html
Last active December 14, 2015 20:59
Slick 404 page
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {
background-color: #333;
font: 60px 'Helvetica, Arial, Ubuntu, sans-serif';
color: #111;
}
h1 {