Skip to content

Instantly share code, notes, and snippets.

View oranj's full-sized avatar
👁️‍🗨️
Observing

Raymond Roeming oranj

👁️‍🗨️
Observing
View GitHub Profile
@oranj
oranj / dump.php
Created September 15, 2011 15:59
dump
if (! function_exists('dump')) {
// Human readable print_r rendered by the browser.
function dump($var, $return = false) {
$str = '<pre style="text-align:left; border:1px solid #ccc; color:#000; background:#fff; margin:10px; padding:10px; background:rgba(255,255,255,0.7);">'.htmlentities(print_r($var, true)).'</pre>';
while (preg_match('/((?!\[\[)([0-9]{10})(?!\]\]))/', $str, $matches)) {
$str = preg_replace('/'.$matches[1].'/', '{<strong>'.date('r', $matches[1])."</strong> <em>[[".$matches[1]."]]</em>}", $str);
}
if ($return) { return $str; } echo $str;
}
}
@oranj
oranj / print_r.js
Created September 15, 2011 17:21
Equivalent of print_r for Javascript
function print_r(obj, ret) {
var start = (new Date()), max_timeout = 100, max_calls = 1000, calls = 0, max_depth = 4, tab = " ";
if (ret == undefined) { ret = false; }
function _indent(depth) { var str = ''; for (var i = 0; i < depth; i++) { str += tab; } return str; }
function _print_r(obj, depth) {
var str = '', now = (new Date()), first = true, t = typeof obj;
if (now - start > max_timeout) { return '('+t+') [[timeout]]'; }
if (calls > max_calls) { return '('+t+') [[max calls]]'; }
if (depth > max_depth) { return '('+t+') [[max depth]]'; }
@oranj
oranj / konami.js
Created September 15, 2011 17:22
Konami Code javascript
var konami_goal = '38 38 40 40 37 39 37 39 66 65 13';
var konami_text = '';
var konami_interval;
var konami_callbacks = {
'update':Array(),
'success':Array(),
'failure':Array(),
'timeout':Array(),
};
@oranj
oranj / filter_ci.js
Created September 21, 2011 15:20
Filter through a select's options with an input (case insensitive)
$.extend($.expr[":"], {
"containsNC": function(elem, i, match, array) {
return (elem.textContent || elem.innerText || "").toLowerCase
().indexOf((match[3] || "").toLowerCase()) >= 0;
}
});
$(function() {
$('[filterselect]').each(function() {
$(this).change(function() {
@oranj
oranj / smartdefault.js
Created September 21, 2011 15:22
Creates simple smart default action with jQuery
$(function() {
$('[smartdefault]').each(function() {
$(this).focus(function() {
if ($(this).hasClass('smartdefault')) {
$(this).val('').removeClass('smartdefault');
// Uncomment the following line to make it stick
// $(this).removeAttr('smartdefault');
}
}).blur(function() {
if (! $(this).val()) {
@oranj
oranj / serila_comma.php
Created September 21, 2011 15:53
Prints serial commas of an array of strings
function serial_comma($array, $serial_comma = true, $conjunction = 'and') {
$size = sizeof($array);
switch($size) {
case 0:
case 1:
return reset($array);
case 2:
return implode(' '.$conjunction.' ', $array);
default:
return implode(', ', array_slice($array, 0, $size - 1)).($serial_comma?',':'').' '.$conjunction.' '.$array[$size - 1];
@oranj
oranj / preg_escape.php
Created September 22, 2011 17:15
PREG Escape
/**
* PREG Escape- escapes regex metacharacters for inserting into a generated regex;
*
* @param String $text The text input to escape
* @return String a regex safe matching string.
*/
function preg_escape($statement) {
$metas = Array('\\', '^', '[', '.', '%', '$', '{', '*', '(', '+', ')', '|', '?', '<', '>', '/', '-', '=', ' ');
foreach ($metas as $char) {
$statement = str_replace($char, '\\'.$char, $statement);
@oranj
oranj / lameroll.js
Created November 14, 2011 14:14
easy banner roller. Pass in a UL.
(function($) {
$.fn.lameroll = function(data) {
this.prefs = $.extend($.fn.lameroll.defaults, data);
this.css({
"width":this.prefs.width,
"height":this.prefs.height,
"list-style-type":"none",
"display":"block",
@oranj
oranj / paramatron.php
Created December 9, 2011 21:05
Regex and harvesting of GET parameters
function get_params($url) {
$param_regex = "/[\?\&]{1}([^=]+)(\=([^\&\=]*))?/";
preg_match_all($param_regex, $url, $matches);
$params = Array();
foreach ($matches[1] as $index => $key) {
$params[$key] = ($matches[2][$index]?$matches[3][$index]:null);
}
return $params;
}
@oranj
oranj / rm_thumb.php
Created December 24, 2011 00:39
Thumbnailer w/ auto crop
<?php
error_reporting(E_ALL);
function getExtension($filename, &$base = NULL) {
$a = split('\.', $filename);
$ext = array_pop($a);
$base = join('.', $a);
return strtolower($ext);
}