Skip to content

Instantly share code, notes, and snippets.

Chris Veness chrisveness

Block or report user

Report or block chrisveness

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@chrisveness
chrisveness / getqueryarg-regexp.js
Created Aug 30, 2014
Get query string argument (using regexp)
View getqueryarg-regexp.js
/**
* Returns specified argument from query string.
*
* @params {string} key - Argument to be returned.
* @returns {string} Value of key ('' for ?arg=, null for ?arg, undefined if not present).
*/
function getQueryArg(key) {
// look for key prefixed by ?/&/;, (optionally) suffixed
// by =val (using lazy match), followed by &/;/# or EOS
var re = new RegExp('[?&;]'+key+'(=(.*?))?([&;#]|$)');
@chrisveness
chrisveness / getqueryarg-splitfor.js
Created Aug 30, 2014
Get query string argument (using split/for)
View getqueryarg-splitfor.js
/**
* Returns specified argument from query string.
*
* @params {string} key - Argument to be returned.
* @returns {string} Value of key ('' for ?arg=, null for ?arg, undefined if not present).
*/
function getQueryArg(key) {
var srch = location.search.substring(1); // lose the initial '?'
var args = srch.split(/[&;]/); // list of field=value pairs
for (var i=0; i<args.length; i++) { // for each arg
@chrisveness
chrisveness / cookie.js
Created Aug 2, 2014
Cookie set / get / delete with safe handling of invalid characters in name & value
View cookie.js
@chrisveness
chrisveness / base64.js
Last active Jun 8, 2019
Encode/decode ASCII string to/from base64
View base64.js
/**
* Encode string into Base64, as defined by RFC 4648 [http://tools.ietf.org/html/rfc4648].
* As per RFC 4648, no newlines are added.
*
* Characters in str must be within ISO-8859-1 with Unicode code point <= 256.
*
* Can be achieved JavaScript with btoa(), but this approach may be useful in other languages.
*
* @param {string} str ASCII/ISO-8859-1 string to be encoded as base-64.
* @returns {string} Base64-encoded string.
@chrisveness
chrisveness / utf8-regex.js
Last active Jan 5, 2019
Utf8 string encode/decode using regular expressions
View utf8-regex.js
/**
* Encodes multi-byte Unicode string into utf-8 multiple single-byte characters
* (BMP / basic multilingual plane only).
*
* Chars in range U+0080 - U+07FF are encoded in 2 chars, U+0800 - U+FFFF in 3 chars.
*
* Can be achieved in JavaScript by unescape(encodeURIComponent(str)),
* but this approach may be useful in other languages.
*
* @param {string} unicodeString - Unicode string to be encoded as UTF-8.
@chrisveness
chrisveness / ago.php
Last active Aug 29, 2015
Describe how long ago something happened in the past
View ago.php
<?php
/**
* Returns how long ago something happened in the past, showing it as
* 'n' seconds / minutes / hours / days / weeks / months / years ago.
*
* For periods over a day, it rolls over at midnight (so doesn't depend on
* current time of day), and it correctly accounts for month-lengths and
* leap-years (months and years rollover on current day of month).
*
@chrisveness
chrisveness / truncate.php
Created Jun 18, 2014
Truncate text to given length with suffixed ellipsis
View truncate.php
<?php
/**
* Truncates text to given length with suffixed ellipsis.
*
* @param string $text Original text string.
* @param int $length Length to truncate to.
* @param bool [$wholeWords=true] Whether to truncate back to whole words.
* @return string Truncated string.
*/
@chrisveness
chrisveness / objencrypt.php
Created Jun 17, 2014
Encrypt/decrypt object to be stored secure from prying eyes
View objencrypt.php
<?php
/**
* Encrypts object to be stored secure from prying eyes (uses AES-256 ECB).
*
* @param object $sourceObj Object to be encrypted.
* @param string $key Key to use for encryption.
* @return string Encrypted object.
*/
function objEncrypt($sourceObj, $key)
@chrisveness
chrisveness / clean.php
Last active Aug 29, 2015
Clean up posted data
View clean.php
<?php
/**
* Cleans up posted data - trims texts & converts empty fields to null.
*
* @param mixed[] $post POST data to be cleaned.
* @return mixed[] Cleaned-up POST data.
*/
function clean($post)
{
@chrisveness
chrisveness / geocode.php
Last active Mar 5, 2019
Geocode an address using Google API
View geocode.php
<?php
/**
* Geocodes an address using Google API (limit 2500/day).
*
* @param string $address - Address to be geocoded.
* @param string [$region=gb] - Region results are to biased to.
* @return object {lat, lon, status, address}.
*/
function geocode($address, $region='gb')
You can’t perform that action at this time.