Skip to content

Instantly share code, notes, and snippets.

@tom--
tom-- / TernaryChain.php
Last active December 14, 2015 01:29
Chaining the PHP ternary
<?php
// Some people object to this use of the ternary. I think it's fine so long as each
// condition-value pair fits nicely on one line. If so then the structure is highly
// expressive. The value $var takes is very obvious. The logic that
// this condition => that value, this condition => that value, etc. is plain to see.
$var =
condition1 ? 'value1' : (
condition2 ? 'value2' : (
condition3 ? 'value3' : (
@tom--
tom-- / regex puzzle.md
Created November 12, 2012 22:22
string replacement

How to implement this substring replacement rule?

  • %% is replaced with % and %x is replaced with #

which would do, for example,

abc %x def      => abc # def
abc %%x def     => abc %x def
abc %%%x def    => abc %# def

abc %%%%x def => abc %%x def

@tom--
tom-- / scratch.sh
Last active October 12, 2015 14:28
Script for safe editing of ipfw rules on FreeBSD using tmux
#!/bin/sh
# What it does:
# - Start tmux
# - Divide the term window into two panes:
# * small lower pane that runs "sleep 60; sudo ipfw disable firewall",
# * large upper pane with a shell in it.
#
# Do your dangerous IPFW work in the upper pane.
# When done, switch to lower pane (Crtl-B, down arrow) and kill it with Ctrl-C
@tom--
tom-- / jquery.editable.js
Created November 3, 2012 12:15
jquery plugin to make contenteditable text trigger a change event when it changes
(function ($) {
'use strict';
/**
* Makes contenteditable elements within a container generate change events.
*
* When you do, e.g. $obj.editable(), all the DOM elements with attribute contenteditable
* that are children of the DOM element $obj will trigger a change event when their
* contents is edited and changed.
*
* See: http://html5demos.com/contenteditable
@tom--
tom-- / needOneOf.php
Created October 1, 2012 14:07
Yii model input validation. Checking that at least one of a given set of attributes is set.
<?php
public function rules() {
return array(
array('a', 'needOneOf', 'or' => array('b', 'c', 'd')),
);
}
public function needOneOf($attr, $params) {
array_unshift($params['or'], $attr);
@tom--
tom-- / thepage.html
Created September 6, 2012 17:31
Unobtrusive ajax link
<html>
<head></head>
<body>
<p><a id="theajaxlink" href="xhrhandler.php?p=1&q=2">text</a></p>
<script type="text/javascript">
(function () {
'use strict';
var clickhandler = function (e) {
var $that = $(this);
e.preventDefault();
@tom--
tom-- / main.php
Created August 30, 2012 16:03
CClientScript component config for a custom jquery.ui
<?php
...
'clientScript' => array(
'class' => 'CClientScript',
'packages' => array(
'jquery.ui' => array(
'basePath' => 'ext.jquery-ui-custom',
'js' => array(
'js/jquery-ui-1.8.18.custom' . (YII_DEBUG ? '' : '.min') . '.js'
@tom--
tom-- / slave_status.php
Created August 17, 2012 18:02
Test a MySQL replication slave's status
#!/usr/local/bin/php
<?php
$error = false;
$errno = 0;
try {
$db = new PDO('mysql:dbname=stations;host=localhost', 'root', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/** @var $slave_status StdClass */
@tom--
tom-- / PhpStorm indent test.php
Created August 9, 2012 12:58
PhpStorm Code: Reformat Code wierdness
<?php
// On my mac, cmd-opt-L reformats my php files according to the code style in effect.
// The following is how PhpStorm auto-formats this statement.
// What do I have to set in the code style so it comes out right?
if (isset(
$_SESSION['one'],
$_SESSION['two'],
$_SESSION['three'],
@tom--
tom-- / recursive-ish regex.php
Created August 7, 2012 18:47
recursive matching braces to a limited depth
<?php
preg_match(
'%'
. preg_quote($marker, '%')
. '\{
(?:[^{}]*\{
(?:[^{}]*\{
(?:[^{}]*\{
(?:[^{}]*\{