Skip to content

Instantly share code, notes, and snippets.

@tom--
tom-- / idecolor.php
Created January 3, 2012 00:10
Convert colorschemedesigner.com XML output to a JetBrains WebIDE color preferences XML file
#!/usr/bin/php
<?php
/**
* @var
*/
// check arguments
if ($argc !== 3 || !is_readable($argv[1]) || !is_readable($argv[2]) ) {
fwrite(STDERR, <<<EOT
Usage: {$argv[0]} csd_export_file ide_color_file
@tom--
tom-- / gist:1665817
Created January 23, 2012 22:16
dateRange helper functions for a Yii CGridView data column
<?php
// this is how to use the dateRange helpers in your view
$this->widget('zii.widgets.grid.CGridView', array(
'afterAjaxUpdate' => Help::dateRangeInitJs(),
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
Help::dateRangeCol($model, 'dateAttr', array('header' => 'The date')),
),
@tom--
tom-- / clean_utf8_inputs.php
Created April 1, 2012 17:06
Utf-8 input handling functions
<?php
function clean_utf8($s) {
return iconv('UTF-8', 'UTF-8//IGNORE', $s);
}
function check_utf8($s) {
return mb_check_encoding($s, 'UTF-8');
}

Say I want to use use a widget in a view thus:

<?php
$this->widget('MyWidget');

And the definition is quite conventional:

@tom--
tom-- / case sensitive file check autoloader.php
Created April 13, 2012 17:41
CS file check in autoloader
<?php
private static function _checkClassPath($path)
{
if (basename(realpath($path . '.php')) !== $path . '.php')
throw new CException("Class name does not match file name.");
}
/**
* Class autoload loader.
* This method is provided to be invoked within an __autoload() magic method.
@tom--
tom-- / hardCreateUrl.php
Created April 27, 2012 15:52
hardUrl helper function
<?php
/**
* Generate an external URL with scheme, hostname and etc. under any(?) circumstances
*
* @param mixed $url Specs for the url, as CHtml::normalizeUrl() would expect
* @param string $scheme 'http' or 'https'
* @throws CException on missing 'extBaseUrl' app param in non-webapp context
* @return string
*/
public static function hardUrl($url, $scheme = 'http') {
@tom--
tom-- / SillyWidget.php
Created June 19, 2012 00:32
A widget with actions
<?php
/**
* This package contains a widget class and two action method classes that the
* widget provides to the CController that uses it.
*
* The controller must register the actions provided by this widget as follows,
* in which we assume this package is in the 'extensions' directory under
* CWebApplication::$basePath
* <code>
@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, '%')
. '\{
(?:[^{}]*\{
(?:[^{}]*\{
(?:[^{}]*\{
(?:[^{}]*\{
@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-- / 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 */