Skip to content

Instantly share code, notes, and snippets.

View wilhelm-murdoch's full-sized avatar
💀
Busy being spoopy.

Wilhelm Murdoch wilhelm-murdoch

💀
Busy being spoopy.
View GitHub Profile
@wilhelm-murdoch
wilhelm-murdoch / Tweet Parser
Created February 1, 2010 13:55
Properly parse tweets using PHP.
function parse($tweet)
{
$regex = array
(
'#([a-z]{3,9}://[a-z0-9-_./\\\?&\+]*)#i',
'#[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}#i',
'#@([a-z0-9-_]+)#i',
'#\#([a-z0-9-_]+)#i'
);
@wilhelm-murdoch
wilhelm-murdoch / Relative Time Function
Created February 2, 2010 12:09
Used to print out the given timestamp in relative format.
function relative($timestamp, $format = 'M jS \of Y')
{
$difference = time() - $timestamp;
if($months = floor($difference / 2592000))
{
return date($format, $timestamp);
}
$difference -= $months * 2419200;
function generate_osid($length)
{
$rand = '';
for($i = 0; $i < $length; $i++)
{
$number = mt_rand(0, 61);
if($number < 10)
{
/**
* Completely strips a string of carriage returns and line feeds.
*
* @param $string String to parse
* @return String
*/
function stripReturns($string)
{
return str_replace(array("\r\n", "\r", "\n"), array("\n", "\n", ''), $string);
}
@wilhelm-murdoch
wilhelm-murdoch / chunkFile(path, chunks);
Created March 12, 2010 08:15
Does a binary-safe split of a specified file. The number of chunks generated is determined by the $chunks parameter.
function chunkFile($file, $chunks = 2)
{
$handle = fopen($file, 'rb');
$count = 1;
while(false == feof($handle))
{
if($data = fread($handle, round(filesize($file) / $chunks)))
{
file_put_contents(basename($file) . "-chunk-{$count}.txt", $data);
@wilhelm-murdoch
wilhelm-murdoch / JSONCountries.json
Created December 6, 2010 02:45
Here is a JSON object which contains a list of all countries and their associated abbreviations. Thought others might find this useful.
[
{"US":"United States"},
{"CA":"Canada"},
{"AF":"Afghanistan"},
{"AL":"Albania"},
{"DZ":"Algeria"},
{"DS":"American Samoa"},
{"AD":"Andorra"},
{"AO":"Angola"},
{"AI":"Anguilla"},
@wilhelm-murdoch
wilhelm-murdoch / outerHtml
Created May 30, 2011 14:12
IE's 'outerHTML' method for jQuery
jQuery.fn.outerHTML = function() {
return $('<div>').append(this.eq(0).clone()).html();
};
@wilhelm-murdoch
wilhelm-murdoch / $.fn.disableSelection();
Created September 16, 2011 05:23
Prevents the "bubbling" effect for text selections in the DOM. For example, when you double-click a block of text on a web page and the entire block gets highlighted and selected. Yeah, this prevents that.
(function($){
$.fn.disableSelection = function() {
return this.each(function() {
$(this).attr('unselectable', 'on')
.css({
'-moz-user-select':'none',
'-webkit-user-select':'none',
'user-select':'none'
})
.each(function() {
@wilhelm-murdoch
wilhelm-murdoch / getChildCategories.php
Created November 17, 2011 14:03
Recursive function for Magento that provides useful information regarding a specified category and all children.
<?php
function getCategories(Mage_Catalog_Model_Category $ParentCategory) {
$return = array();
foreach(explode(',', $ParentCategory->getChildren()) as $categoryId) {
$Category = Mage::getModel('catalog/category')->load($categoryId);
$return[$categoryId] = array(
'id' => $Category->getId(),
'name' => $Category->getName(),
'slug' => $Category->getUrlKey(),
'url' => $Category->getUrl(),
@wilhelm-murdoch
wilhelm-murdoch / getPureRotation.js
Created December 18, 2011 05:22
A jQuery method that attempts to calculate the angle of rotation of the given HTML element.
$.fn.getPureRotation = function(element) {
var degrees = null;
$.each(['-webkit-transform', '-moz-transform', '-o-transform', '-sand-transform', '-ms-transform', 'transform'], function(index, value) {
var matrix = $(element).css(value);
if(degrees == null || Boolean(matrix)) {
var arrMatrix = matrix.match(/[\-0-9.]+/g);
if(
(parseFloat(arrMatrix[1]) == (-1 * parseFloat(arrMatrix[2]))) ||
(parseFloat(arrMatrix[3]) == parseFloat(arrMatrix[0])) ||
((parseFloat(arrMatrix[0]) * parseFloat(arrMatrix[3]) - parseFloat(arrMatrix[2]) * parseFloat(arrMatrix[1])) == 1)