Skip to content

Instantly share code, notes, and snippets.

View troelskn's full-sized avatar

Troels Knak-Nielsen troelskn

View GitHub Profile
<?php
function mysql_bind($sql, $params = array()) {
$buffer = array();
$tokens = preg_split('/(:[a-z0-9_]+)/', $sql, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
foreach ($tokens as $token) {
if (preg_match('/:[a-z0-9_]+/', $token)) {
if (!array_key_exists($token, $params)) {
trigger_error("Missing parameter '$token'");
} elseif (!isset($params[$token])) {
$buffer[] = "null";
<?php
class Foo {
protected static $blah;
static function stuff() {
static $cuux = null;
if ($cuux === null) {
echo "It's null.\n";
$cuux = 42;
} else {
echo "It's not null anymore.\n";
/**
* Wrapper to execute a remote command over ssh.
* @param $ssh Connection from ssh2_connect
* @param $command string to execute on shell
* @returns string
*/
function ssh2_exec_command($ssh, $command, $retry = 5) {
// The `sleep` is a nasty hack to account for some kind of timing bug in ssh2
$sleep = 0;
$stream = false;
def html_to_xml(html)
filename = "/tmp/tidyfy-#{ Time.now.strftime('%Y%m%d%H%M%S') }.html"
File.open(filename, 'w') {|f| f << html }
output = `tidy -asxml -numeric --output-encoding utf8 --doctype omit --add-xml-decl true #{filename} 2> /dev/null`
output.strip.gsub(/<html([^>]*)>/, "<html>")
end
<?php
function notifo_notify($label, $title, $msg, $uri, $user, $pass) {
$opts = array(
'http' => array(
'method' => "POST",
'header' => "Authorization: Basic ".base64_encode($user.":".$pass)."\r\n".
"Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query(array('label' => $label, 'title' => $title, 'msg' => $msg, 'uri' => $uri))
)
);
#!/usr/bin/env bash
# Note the use of env. This is because executables may not be placed in the same place on all systems. Notoriously, bsd (and thus mac osx) differs from linux. However, /usr/bin/env always exists in the same place, and it can tell you where other executables are. Thus, the above is good style for portability.
COMMIT_VERSION=$(git rev-parse --verify HEAD)
# You could also use backticks, but the $() syntax is generally more readable
# Convention is to put variables in uppercase in shell script
curl -k -u user:password \
-d "to=lsolesen&msg=%22Intraface+build+failed%22&title=Intraface+build+failed&uri=https%3A%2F%2Fgithub.com%2Fintraface%2Fintraface.dk%2Fcommit%2F${COMMIT_VERSION}" \
https://api.notifo.com/v1/send_notification > /dev/null
@troelskn
troelskn / underscore.php
Created December 22, 2010 13:45
camelize + underscore in php
<?php
/**
* Transforms an under_scored_string to a camelCasedOne
*/
function camelize($scored) {
return lcfirst(
implode(
'',
array_map(
#!/bin/bash
#
# Usage:
# PADDING=10 HTML_NORMAL=normal.html HTML_HOVER=hover.html ./webkit_sprite
#
if [ -z "$PADDING" ]
then
echo "You need to specify PADDING"
exit -1
fi
@troelskn
troelskn / gist:773216
Created January 10, 2011 18:40
json_encode_pretty
<?php
/**
* Input an object, returns a json-ized string of said object, pretty-printed
*
* @param mixed $obj The array or object to encode
* @return string JSON formatted output
*/
function json_encode_pretty($obj, $indentation = 0) {
switch (gettype($obj)) {
case 'object':
@troelskn
troelskn / jquery.flexheight.js
Created February 12, 2011 11:33
Make an element extend to full remaining height of its parent, minus height consumed by siblings. For 100% height layouts
// Make an element extend to full remaining height of its parent, minus height consumed by siblings. For 100% height layouts
// Usage:
// $(document).ready(function() {
// $(".layout-flex-height").flexHeight();
// });
(function($) {
$.fn.flexHeight = function() {
var context = this;
var handleFlexLayout = function () {
context.each(function() {