Skip to content

Instantly share code, notes, and snippets.

<?php
/**
* Search a block of text for a given text string
*
* @param string $pattern
* A regexp to match against
* @param string|array $input
* A string or array (can't handle recursiveness) to search
* @param int $context
* Returns x number of lines surrounding the hit
<?php
/**
* Make MySQL correctly sort nodes on swedish sites
*/
function hook_install() {
db_query("ALTER TABLE {node} MODIFY
title VARCHAR(255)
CHARACTER SET utf8
COLLATE utf8_swedish_ci");
}
<?php
// One liner to kill the bastards.
preg_replace('/\s(?!.*\s)/', '&nbsp;', $string);
@simme
simme / char_counter_deluxe.js
Created October 19, 2010 09:23
Count characters
// Character counter
$('#edit-field-tagline-0-value, #edit-body').keydown(function(event) {
var id = $(this).attr('id');
var counter = $('label[for="' + id + '"]').children('span');
if (!counter.length) {
$('label[for="' + id + '"]').append('<span></span>');
counter = $('label[for="' + id + '"]').children('span');
}
counter.text(' (Antal tecken: ' + $(this).val().length + ')');
@simme
simme / Find Mate
Created October 25, 2010 14:51
Small bash function for opening a set of files in mate based on a search pattern
#!/usr/bin/env bash
# Usage: fm nameoffile.php
# Searches the current dir you're in
function fm {
find . -name "$1" -exec mate {} \;
}
<?php
/**
* @file
* Deploy site backups as new sites on a new server.
* Duplicate platform before doing this.
*/
// Path to directory containing backups
define('BACKUP_PATH', '/var/aegir/backups');
@simme
simme / ChangeLanguage.js
Created November 24, 2010 15:21
Client side language switching
var language = (function ($) {
var current = 'en'
, languages = { sv: {}
, da: {}
, en: {}
}
, self = {};
/**
* Set the language strings
@simme
simme / Base64Image.php
Created December 30, 2010 09:57
Returns the Base64 encoded version of an image. Has basic support for Drupal module ImageCache. Does not depend on Drupal.
<?php
/**
* @file
* Helper class for encoding images as Base64.
* Has support for the Drupal module ImageCache, just provide it with your
* preset name if you'd like the class to use that version of an image instead.
* Is by no means dependent on Drupal though. Just a nice feature.
* Feel free to use it as you like.
*
* @author Simon Ljungberg <simon@nimnim.se>
@simme
simme / drupal_language_update.php
Created April 12, 2011 12:21
Update translations on a Drupal site
<?php
// Update translations
// This might need some 'splainin':
// RecursiveDirectoryIterator together with RecursiveIteratorIterator
// loops through all files in the give root directory.
// RegexIterator filters out the files we want (modules-topbar and
// themes-xskane) translations in this case. Matches the languages code
// and filename. Then we loop through it and win.
$di = new RecursiveDirectoryIterator('./sites/all/translations/');
$it = new RecursiveIteratorIterator($di);
@simme
simme / HTMLWeld.js
Created April 13, 2011 07:09
Welds data with HTML
/**
* @file
* Generates some various HTML stuffs.
*/
var fs = require('fs')
, jsdom = require('jsdom')
;
/**
* Welds some HTML together with a bunch of data