Skip to content

Instantly share code, notes, and snippets.

View craiga's full-sized avatar
🤓

Craig Anderson craiga

🤓
View GitHub Profile
@craiga
craiga / Logging.php
Last active September 30, 2015 19:18
printf-esque log method
<?php
trait Logging {
/**
* Write a log message.
*
* Write a log message to $this->_logCallback, $this->_logFile and/or
* syslog.
* Parameters are as for {@link http://php.net/sprintf sprintf}, except
* that a syslog priority (LOG_EMERG to LOG_DEBUG; see http://php.net/syslog)
@craiga
craiga / LogDump.php
Last active October 4, 2015 21:07
_logDump
<?php
/**
* Dump the output of var_dump to a log file.
*
* Dump the output of var_dump to a log file. Requires {@link https://gist.github.com/1849563 this _log function}.
*
* @author Craig Anderson <craiga@craiga.id.au>
* @link https://gist.github.com/gists/2699763
*/
protected function _logDump($var)
@craiga
craiga / slugify.php
Created May 17, 2012 03:55
slugify
<?php
/**
* Slugify a string.
*
* Convert "Hello, world! It's Craig in 2012." to "hello-world-its-craig-in-2012".
* See test_slugify for more examples.
*
* @author Craig Anderson <craiga@craiga.id.au>
* @link https://gist.github.com/2716157
@craiga
craiga / GetUrl.php
Last active November 18, 2015 23:30
Get a URL.
<?php
require_once("GetUrlHttpErrorException.php");
include_once(dirname(__FILE__) . "/../removeOldFiles/removeOldFiles.php");
/**
* Get a URL.
*
* Get a URL, optionally caching the response. Will remove old cached files if {@link https://gist.github.com/craiga/3161529 removeOldFiles} is present.
*
@craiga
craiga / format_memory.php
Last active October 5, 2015 21:37
Format a number of bytes.
<?php
/**
* Format a number of bytes.
*
* Format a number of bytes into a readable string in either IETF (kibibyte,
* 1024 bytes) or SI (kilobyte, 1000 bytes) units.
* Produces output like "1 byte", "28 bytes", "4 MB" or "22 GiB".
*
* @param $memory The number of bytes to format. The result of
@craiga
craiga / printXml.php
Last active October 6, 2015 04:08
printXml
<?php
/**
* Print some XML.
*
* FIXME: This probably doesn't do a good job at prettifying whitespace when supplied with a DOMNode.
*
* @author Craig Anderson <craiga@craiga.id.au>
* @link https://gist.github.com/2934093
*/
@craiga
craiga / normaliseUrl.php
Last active October 6, 2015 11:28
normaliseUrl
<?php
/**
* Normalise a URL.
*
* Normalise a URL. Returns null if the URL couldn't be normalised.
*
* @author Craig Anderson <craiga@craiga.id.au>
* @link https://gist.github.com/2986522
*/
@craiga
craiga / setup-server.sh
Last active October 6, 2015 12:28
Setting up a debuntu server
# get ip address to put into your workstation's /etc/hosts
sudo ifconfig eth0
# set up ssh (courtesy of Doug)
sudo aptitude install openssh-server
sudo sed -i 's/^ChallengeResponseAuthentication.*$/ChallengeResponseAuthentication yes/g' /etc/ssh/sshd_config
sudo service ssh restart
# tab completion
sudo aptitude install bash-completion
@craiga
craiga / removeOldFiles.php
Created July 23, 2012 00:49
removeOldFiles
<?php
/**
* Remove old files.
*
* Remove files older than one day (or the age specified). Will not remove old directories.
*
* @param $directory The directory to remove files from.
* @param $recursive Whether to remove files from subdirectories as well. False by default.
* @param $age The minimum age of a file before it is removed. One day by default.
@craiga
craiga / formatTime.php
Last active October 7, 2015 15:28
formatTime
<?php
/**
* Format a number of seconds.
*
* Format a number of seconds into a number of seconds, minutes, hours and days.
* Produces output like "1 second", "110 seconds", "4 hours" or "123 microseconds".
* The accuracy gets a little rough once it gets into months, but it's only meant
* as an approximation.
*