Skip to content

Instantly share code, notes, and snippets.

View craiga's full-sized avatar
🤓

Craig Anderson craiga

🤓
View GitHub Profile
<?php
/**
* Flatten an array, preserving keys.
*
* @author Craig Anderson <[email protected]>
* @link https://gist.github.com/craiga/5855194
*/
function flattenArray($in) {
$out = array();
@craiga
craiga / enthusiasticurlencode.php
Last active December 13, 2015 23:29
Enthusiastic URL encoding. URL encodes everything that's not an alphanumeric character.
<?php
/**
* Enthusiastically URL encode a string.
*
* URL encode everything that's not an alphanumeric character.
*
* @author Craig Anderson <[email protected]>
* @link https://gist.github.com/craiga/4991769#file-enthusiasticurlencode-php
*/
/**
* Parse query string into an object.
*
* @author Craig Anderson <[email protected]>
* @link https://gist.github.com/4760400
*/
function parseQueryString(unparsed) {
if(typeof unparsed == "undefined") {
unparsed = window.location.search;
}
<?php
/**
* Dump a simple view of the current call stack.
*
* @author Craig Anderson <[email protected]>
* @link https://gist.github.com/4687895
*/
function stackdump() {
$stack = debug_backtrace();
foreach($stack as $stackIndex => $stackEntry) {
@craiga
craiga / parseCookies.js
Created November 23, 2012 05:42
parseCookies.js
/**
* Parse cookies into an object.
*
* @author Craig Anderson <[email protected]>
* @link https://gist.github.com/4134160
*/
function parseCookies(unparsed) {
if(typeof unparsed == "undefined") {
unparsed = document.cookie;
}
@craiga
craiga / randomstring.js
Created November 14, 2012 22:45
randomstring.js
/**
* Generate a random string.
*
* @param alphabet (optional) The alphabet used to generate a random string.
* @param minLength (optional) The minimum length of the string. 10 by default.
* @param maxLength (optional) The maximum length of the string. 20 by default.
*
* @author Craig Anderson <[email protected]>
* @link https://gist.github.com/4075392
*/
@craiga
craiga / htmlencode.js
Created October 1, 2012 06:32
HTML Encode for JavaScript
/**
* Based on http://stackoverflow.com/a/6020820/323826
*/
var htmlEscapes = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#x27;',
@craiga
craiga / strtobytes.php
Created September 21, 2012 01:49
strtobytes
<?php
/**
* Parse a description of a number of bytes to an actual number of bytes.
*
* @author Craig Anderson <[email protected]>
* @link https://gist.github.com/3759346
*/
function strtobytes($dataSize)
{
@craiga
craiga / LogProgress.php
Last active October 9, 2015 05:18
_logProgress
<?php
/**
* Log progress.
*
* Log progress with a message along the lines of "Processed 600 of 20,000 items (3%)."
* Makes use of {@link https://gist.github.com/1849563 _log}.
*
* @author Craig Anderson <[email protected]>
* @link https://gist.github.com/3444731
@craiga
craiga / formatInt.js
Created August 23, 2012 07:55
formatInt
function formatInt(num) {
num = parseInt(num);
str = new String(num);
if(str.length > 4) {
var formatted = "";
do {
var lastChar = str.length;
formatted += "," + str.slice(lastChar - 3);
str = str.slice(0, lastChar - 3);
} while(str.length > 3);