Skip to content

Instantly share code, notes, and snippets.

@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-- / 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-- / 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-- / 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-- / 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-- / 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-- / 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-- / detect url regex.php
Last active December 14, 2015 02:49
detecting urls
<?php
$letnum = '\p{L}\p{Mn}\p{Mc}\p{Nd}';
$sym = '+&@#/%=~_|$';
$pnct = '?!:,.-';
$c1 = "[$letnum$sym$pnct]";
$c2 = "[$letnum$sym]";
// then the regex pattern is, so to speak:
// \b(?: https?:// | www\. ) (?: \($c1*\) | $c1 )* (?: \($c1*\) | $c2 )
// with xui options.
@tom--
tom-- / nowplaying.html
Last active December 14, 2015 13:49
Example of styling the text in a Spinitron "now playing" iframe. Description is in a comment below.
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Spinitron Playlists</title>
</head>
<body>
<p class="recentsong">
<span class="songpart"><b>Wave Guidance Allows Three</b></span>
<span class="artistpart">by <b><a target="_top" href="/public/index.php?station=wzbc&amp;dbdetail=a&amp;dbid=83733">The Swifter</a></b></span>
@tom--
tom-- / Clean up stray & in XML.php
Last active December 15, 2015 20:29
Cleaning up stray & chars in a verkakte XML input document.
<?php
$input = preg_replace(
'{& (?! \#x[\da-f]{2,6} ; )
(?! \#\d{3,6} ; )
(?! [a-z0-9]{2,16} ; )
}xiu',
'&amp;',
$input
);