Skip to content

Instantly share code, notes, and snippets.

View davebarnwell's full-sized avatar

Dave Barnwell davebarnwell

View GitHub Profile
@davebarnwell
davebarnwell / truncateString.js
Created December 14, 2014 11:51
truncate string on word boundary
function truncate(str, len) {
if (str.length > len) {
var new_str = str.substr (0, len+1);
while (new_str.length) {
var ch = new_str.substr ( -1 );
new_str = new_str.substr ( 0, -1 );
if (ch == ' ') {
break;
@davebarnwell
davebarnwell / stripslashes.php
Created January 5, 2015 19:38
strip slashes from input if PHP magic quotes is enabled
<?php
// if magic quotes are on strip slashes from vars
if (get_magic_quotes_gpc()) {
function stripslashes_array(&$arr) {
foreach ($arr as $k => &$v) {
$nk = stripslashes($k);
if ($nk != $k) {
$arr[$nk] = &$v;
unset($arr[$k]);
}
@davebarnwell
davebarnwell / uk_postcode.php
Created January 23, 2015 14:46
Clean and validate UK postcode
<?php
/**
* Validate and parse UK post code
*
* @param string $postcode
* @return array(["validate"] => TRUE/FALSE
* ["prefix"] => first part of postcode
* ["suffix"] => second part of postcode);
*/
function inspect_postcode($postcode) {
@davebarnwell
davebarnwell / LICENSE
Last active August 29, 2015 14:15 — forked from jdennes/LICENSE
Campaign monitor Ajax submit
The MIT License (MIT)
Copyright (c) James Dennes
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
@davebarnwell
davebarnwell / jQuery.smooth.js
Created February 22, 2015 12:20
jQuery smooth scroll to anchor
jQuery(document).ready(function ($){
// smooth scroll to anchor on page
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
var scrollBy = target.offset().top;
$('html,body').animate({
// Support routines for automatically reporting user timing for common analytics platforms
// Currently supports Google Analytics, Boomerang and SOASTA mPulse
// In the case of boomerang, you will need to map the event names you want reported
// to timer names (for mPulse these need to be custom0, custom1, etc) using a global variable:
// rumMapping = {'aft': 'custom0'};
(function() {
var wtt = function(n, t, b) {
t = Math.round(t);
if (t >= 0 && t < 3600000) {
// Google Analytics
#!/bin/sh
# Pre-commit hook for git which removes trailing whitespace, converts tabs to spaces, and enforces a max line length.
# The script does not process files that are partially staged. Reason: The `git add` in the last line would fully
# stage a file which is not what the user wants.
if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
@davebarnwell
davebarnwell / 302cookie.php
Last active November 3, 2015 08:18
Test cookie set on 302 redirect
<?php
/**
* Test for cookie setting when doing a 302 redirect,
* has been known to cause issues in certain browser versions most notible Safari.
*
* Copied from here http://blog.dubbelboer.com/2012/11/25/302-cookie.html
*/
if (isset($_GET['show'])) {
// Delete the cookie (note: it will still be set in $_COOKIE).
setcookie('test');
@davebarnwell
davebarnwell / debouce.js
Created November 3, 2015 20:26
function to return a debouced js function which honours the orginal call context
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
@davebarnwell
davebarnwell / index.html
Last active November 6, 2015 13:39
javascript countdown timer
<html>
<head>
<title>count down clock</title>
<style>
body{
text-align: center;
background: #00ECB9;
font-family: sans-serif;
font-weight: 100;
}