Skip to content

Instantly share code, notes, and snippets.

View flesch's full-sized avatar

John Flesch flesch

View GitHub Profile
@flesch
flesch / setSize.js
Created October 27, 2009 21:26
Set a window's size
function setSize(w, h) {
if (window.name && w && h) {
var innerWidth, innerHeight, width = Math.floor(w), height = Math.floor(h);
innerWidth = (window.innerWidth || document.documentElement.clientWidth);
innerHeight = (window.innerHeight || document.documentElement.clientHeight);
window.resizeBy(Math.floor(width-innerWidth), Math.floor(height-innerHeight));
window.moveTo(Math.floor((screen.availWidth/2)-(width/2)), Math.floor((screen.availHeight/2)-(height/2)));
}
}
@flesch
flesch / benchmarking.php
Created February 25, 2010 20:16
PHP Benchmarking
<?php
function utime() {
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
// Start benchmarking.
$start = utime();
@flesch
flesch / sprintf.as
Created February 25, 2010 21:46
Very simple sprintf() implementation
function sprintf(){
var args = Array.prototype.slice.call(arguments), f:Array = [], str = args.shift().split("%s");
while (str.length) {
f.push(str.shift(), args.shift() || "");
}
return f.join("");
}
trace(sprintf("Hello %s, how ya %s?", "motherf*cker", "durin"));
@flesch
flesch / guid.js
Created August 3, 2010 17:38
Token
function guid() {
function hex(len) {
var s = '', n = len || 4;
while (s.length < n) {
s += (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
return s.substr(0, n);
}
return [hex(8), hex(4), hex(4), hex(4), hex(12)].join('-');
}
@flesch
flesch / get_http_status_header.php
Created August 10, 2010 14:31
Return the HTTP Status code as an integer.
<?php
function get_http_status_header($url) {
preg_match('/[0-9]{3}/', array_shift(get_headers($url)), $matches);
return (int) array_shift($matches);
}
?>
@flesch
flesch / boilerplate.html
Created November 19, 2010 04:05
A quick HTML5 boilerplate
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>fles.ch</title>
<!--[if IE]>
<script src="http://static.fles.ch/js/html5.js"></script>
<![endif]-->
<link rel="stylesheet" href="http://static.fles.ch/css/reset-min.css" />
<style>
@flesch
flesch / boilerplate.php
Created November 19, 2010 04:11
A quick PHP/HTML5 boilerplate.
<?php
define('APP_ROOT', pathinfo(implode('/', array_slice(explode('/', $_SERVER['PHP_SELF']), 1, 2)), PATHINFO_DIRNAME));
define('DOCUMENT_ROOT', str_replace('/.', '', sprintf('%s/%s', $_SERVER['DOCUMENT_ROOT'], APP_ROOT)));
define('SERVER_NAME', preg_replace('#^www\.#', '', $_SERVER['SERVER_NAME']));
define('SERVER_URL', sprintf('http://%s/%s', SERVER_NAME, APP_ROOT));
function utime() {
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
@flesch
flesch / router.php
Created December 3, 2010 03:49
Simple PHP router.
<?php
/*******************************************************************************
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
*******************************************************************************/
@flesch
flesch / async_request.php
Created March 15, 2011 15:47
Non-blocking background HTTP request
<?php
function async_request($url) {
$request = parse_url($url);
if (!isset($request['port'])) {
$request['port'] = 80;
}
$fp = @fsockopen($request['host'], $request['port'], $errno, $errstr, 30);
if ($fp) {
fwrite($fp, implode("\r\n", array(
@flesch
flesch / str_to_time.coffee
Created December 15, 2011 17:27
str_to_time / time_to_str
# Convert string to a timestamp.
# str_to_time("01:20") = 80
str_to_time = (str) ->
str.replace /^([0-9]{1,2}):([0-9]{2})$/, (t, m, s) -> (m* 60) + parseFloat(s)