Skip to content

Instantly share code, notes, and snippets.

View dave1010's full-sized avatar

Dave Hulbert dave1010

View GitHub Profile
@dave1010
dave1010 / google-analytics-eu-cookie.js
Created May 26, 2011 08:37
Google Analytics loading script that complies with the new EU cookie legislation
@dave1010
dave1010 / cowsay.txt
Created June 10, 2011 08:48
for i in `ls /usr/share/cowsay/cows`; do echo $i && cowsay -f $i hi; done > cowsay.txt
apt.cow
____
< hi >
----
\ (__)
(oo)
/------\/
/ | ||
* /\---/\
~~ ~~
@dave1010
dave1010 / to-string.php
Created June 10, 2011 15:02
How to throw exceptions in a PHP class' __toString method
<?php
class MyClass {
public function __toString() {
try {
return $this->render();
} catch(Exception $e) {
// the __toString method isn't allowed to throw exceptions
// so we turn them into an error instead
trigger_error($e->getMessage() . "\n" . $e->getTraceAsString(), E_USER_ERROR);
@dave1010
dave1010 / firebug-to-iframe.js
Created June 13, 2011 11:02
Make Firebug's commandline execute commands in an iframe
cd($$('iframe')[0].contentWindow)
@dave1010
dave1010 / HtmlNode.php
Created August 9, 2011 14:02
HtmlNode.php - generate HTML from PHP
<?php
/**
* Quick and dirty HTML generation for PHP
*/
class HtmlNode {
public $element = 'div';
public $content = null;
public $attrs = array();
public function __construct($element=null, $content=null, $attrs=null, $closing=null, $shortclosing=' /') {
@dave1010
dave1010 / 00-the-wordpress-way.md
Created August 18, 2011 09:21
The WordPress Way - The 10 rules of WordPress development

The WordPress Way

A tongue-in-cheek look at coding standards in the WordPress core and the average WordPress plugin.

  1. # Declare variables global - in case they're going to be used again
  2. All function/method parameters should be strings (e.g. 'yes'/'no') - for clarity
  3. Functions and methods should return mixed types
  4. No need to separate PHP logic from HTML, JS or CSS
  5. Don't worry about PHP Notices - they're not important
@dave1010
dave1010 / hnpanel.js
Created August 23, 2011 11:51
the code from @chetan51 's hnpanel bookmarklet
(function(e, a, g, h, f, c, b, d) {
if (! (f = e.jQuery) || g > f.fn.jquery || h(f)) {
c = a.createElement("script");
c.type = "text/javascript";
c.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + g + "/jquery.min.js";
c.onload = c.onreadystatechange = function() {
if (!b && (!(d = this.readyState) || d == "loaded" || d == "complete")) {
h((f = e.jQuery).noConflict(1), b = 1);
f(c).remove()
}
@dave1010
dave1010 / php-regex-router.php
Created September 13, 2011 15:46
php-regex-router.php
<?php
// dangerously simple PHP regular expression URL router
// requires a mod_rewrite like "RewriteRule . /index.php [L]"
function get($url, $callback) {
$matches = array();
if (preg_match('~' . $url . '~', $_SERVER['REQUEST_URI'], $matches)) {
echo call_user_func_array($callback, $matches);
die();
}
@dave1010
dave1010 / jquery.commentToHTML.js
Created October 10, 2011 15:40
jQuery plugin: set the HTML of an element to be the contents of its first comment
/*global $ */
/**
* Replace an element's HTML with the content of its first comment
*/
$.fn.commentToHTML = function() {
var d = 'extracted-comments';
return this.each(function(i, el) {
var $el = $(this);
@dave1010
dave1010 / pre-commit
Created October 28, 2011 12:31
pre-commit PHP link and check for merge conflicts
#!/bin/bash
conflicts=`git diff --cached --name-only -S'<<<<<<'`
for i in `git diff --cached --name-only | grep '.php'`;
do
thisphperror=`php -l $i | grep -v 'No syntax errors detected in'`
if [ -n "$thisphperror" ]; then
echo "PHP errors added in file: " $i