Skip to content

Instantly share code, notes, and snippets.

View beporter's full-sized avatar

Brian Porter beporter

View GitHub Profile
@beporter
beporter / select_options.php
Last active August 29, 2015 13:57
Handle PHP associative and indexed array in a single loop
<?php
//--------------------
function brian($i) {
foreach ($i as $value => $name) {
$value = (is_string($value) ? $value : $name);
echo '<option value="' . $value . '">' . $name . '</option>' . PHP_EOL;
}
}
@beporter
beporter / human_filesize.php
Created March 27, 2014 22:50
Convert an integer byte value into a human-readable file size.
<?php
//----------
// Only works with INTEGER values for $bytes.
function human_filesize($bytes, $decimals = 2) {
$sz = 'BKMGTP';
$bytes = (int)$bytes; // This process only works with integer $byte values.
$factor = floor((strlen($bytes) - 1) / 3); // Determine the correct range.
$s = $bytes / pow(1024, $factor); // Knock out the right number of exponents.
$s = sprintf("%.{$decimals}f", $s) + 0; // Clean up decimals, `+0` removes trailing zeroes.
@beporter
beporter / php_empty_iterator_test.txt
Last active August 29, 2015 14:01
Attempting to call PHP's `EmptyIterator->current()`
$ php -r '$i = new EmptyIterator(); echo $i->current();'
PHP Fatal error: Uncaught exception 'BadMethodCallException' with message
'Accessing the value of an EmptyIterator' in Command line code:1
Stack trace:
#0 Command line code(1): EmptyIterator->current()
#1 {main}
thrown in Command line code on line 1
Fatal error: Uncaught exception 'BadMethodCallException' with message
@beporter
beporter / bash_prompt.sh
Created June 2, 2014 14:57
Bash Prompt
. /etc/bashrc # Pull in the update_terminal_cwd() function.
export PROMPT_COMMAND="prompt_cmd; update_terminal_cwd"
function prompt_cmd {
# Must be obtained early before any other commands run and exit!
EXITSTATUS="$?"
# regular colors
local K="\[\033[0;30m\]" # black
local R="\[\033[0;31m\]" # red
@beporter
beporter / README.md
Last active August 29, 2015 14:03
Provides a pre-composer mechanism for setting up a copy of the CakePHP core project in a local directory and then symlinking it into a target Cake project path.

CakePHP Core Setup

These scripts automate the process of obtaining a copy of the CakePHP core at a specific point release, the latest point release for a minor version, or a specific commit. They also provide a way to symlink the Cake core into an existing Cake project, which is handy for keeping all of the core files out of your project repo.

Usage

  • Place add-cakephp-version.sh in a folder of its own.

  • Run ./add-cakephp-version.sh x.y[.z] (where x is a major version number, y is a minor version number and z is an optional point release) to clone the CakePHP official git repository and set up a folder named cake_x.y.z in the same folder as the script.

@beporter
beporter / BasicIterator.php
Created August 6, 2014 16:42
PHP Iterator Tutorial
<?php
class BasicIterator extends FilterIterator {
public function __construct($pathToFile) {
// Call the parent constructor with an SplFileObject (also Traversable) for the given path.
parent::__construct(new SplFileObject($pathToFile, 'r'));
// These set up the inner SplFileObject's properties to process CSV.
$file = $this->getInnerIterator();
$file->setFlags(SplFileObject::READ_CSV);
@beporter
beporter / monitor-500.sh
Last active August 29, 2015 14:08
Monitor a URL for HTTP 500 errors using a crude shell script.
#!/usr/bin/env bash
#
# Takes a URL as an argument. Will test that URL every X seconds for a
# 500 response code. If a 500 is returned, this script will ring the
# terminal's bell and print a message.
#
# Ref: http://beerpla.net/2010/06/10/how-to-display-just-the-http-response-code-in-cli-curl/
#
# Usage: ./monitor-500.sh "http://yoursite.com/your/page.html"
#
@beporter
beporter / nostalgia.c
Created October 29, 2014 13:35
Decided to see if I could still write C from memory.
// Compile: `gcc -Wall -o hello nostalgia.c`
// Run: `./hello`
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv) {
printf("Hello world.\n");
return(0);
}
@beporter
beporter / keybase.md
Created November 5, 2014 21:32
Proving GitHub identity for Keybase.

Keybase proof

I hereby claim:

  • I am beporter on github.
  • I am beporter (https://keybase.io/beporter) on keybase.
  • I have a public key whose fingerprint is A6BB 0797 14AB 98D6 302E 340F CFDE 3154 EF8B 48D3

To claim this, I am signing this object:

@beporter
beporter / git-find-repos
Created August 10, 2015 22:04
Find all git repos (folders containing a `.git/` subdirectory) in the provided path (defaults to current directory.)
#!/usr/bin/env bash
#
# Find and list all git repos (folders containing .git/ directories)
# in the provided path (defaults to current dir.)
#
# Designed to run on a Mac, so make sure you have GNU `findutils`
# and the `terminal-notifier` gem installed.
#
# beporter@users.sourceforge.net
# v1.0 2015-08-10