Skip to content

Instantly share code, notes, and snippets.

View craiga's full-sized avatar
🤓

Craig Anderson craiga

🤓
View GitHub Profile
@craiga
craiga / LogMemoryUsage.php
Last active January 11, 2025 20:33
_logMemoryUsage
<?php
/**
* Log memory usage.
*
* Makes use of {@link https://gist.github.com/1849563 _log} and {@link https://gist.github.com/2880386 formatMemory}.
*
* @author Craig Anderson <[email protected]>
* @link https://gist.github.com/3336985
*/
protected function _logMemoryUsage()
@craiga
craiga / getElementId.js
Last active October 8, 2015 04:48
getElementId
/**
* Get the ID of an element.
*
* Get the ID of an element, generating one if none exists and the second
* parameter is set to true.
* Makes use of {@link https://gist.github.com/craiga/4075392 randomString}.
*
* @author Craig Anderson <[email protected]>
* @link https://gist.github.com/3279884
*/
@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.
*
@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 / 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 / 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 <[email protected]>
* @link https://gist.github.com/2986522
*/
@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 <[email protected]>
* @link https://gist.github.com/2934093
*/
@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 / 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 / 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 <[email protected]>
* @link https://gist.github.com/2716157