Skip to content

Instantly share code, notes, and snippets.

View flesch's full-sized avatar

John Flesch flesch

View GitHub Profile
@flesch
flesch / sequence-as2.as
Created August 18, 2012 15:12
Convert to and from a binary sequence
function str2seq(str:String):String {
var seq:String = "", byte:String, len:Number = str.length, i:Number = 0;
for (; i<len; i++) {
byte = parseInt(ord(str.charAt(i)), 10).toString(2);
seq += "00000000".substr(0, 8-byte.length) + byte;
}
return seq;
}
function seq2str(seq:String):String {
@flesch
flesch / list_dir.php
Created August 18, 2012 15:00
Recursive Directory Listing in PHP
<?php
function list_dir($resource) {
$files = array();
$scan = glob(rtrim($resource, '/') . '/*');
if (is_file($resource)) {
array_push($files, $resource);
}
if (is_dir($resource)) {
foreach ($scan as $path) {
@flesch
flesch / getElementsByClassName.php
Created August 18, 2012 14:49
document::getElementsByClassName("")
<?php
class document {
public static function getElementsByClassName($name) {
global $xpath;
$nodes = array();
$elements = $xpath->query(sprintf("//div[contains(@class, '%s')]", $name));
foreach ($elements as $e) {
array_push($nodes, $e);
}
@flesch
flesch / README.md
Created July 28, 2012 03:44
Fake SCORM API

Fake SCORM API

Some courses won't work outside of an LMS. That's annoying when you just want to review a course, so drop this in to provide a fake SCORM API implementation - getAPI should then pick up this fake API.

@flesch
flesch / getQueryParam.js
Last active October 6, 2015 23:38
Query String Memoization
function getQueryParam(param) {
if (!this.params) {
this.params = (function (query) {
var obj = {};
if (query) {
var i = query.length, keyValuePair;
while (i--) {
keyValuePair = query[i].split('=');
if (keyValuePair[0] && keyValuePair[1]) {
obj[keyValuePair[0]] = keyValuePair[1];
@flesch
flesch / extend.js
Created July 3, 2012 15:12
Shallow Copy of an Object
function extend(a, b) {
var c = {}, attr;
for (attr in a) {
if (Object.prototype.hasOwnProperty.call(a, attr)) {
c[attr] = a[attr];
}
}
for (attr in b) {
if (Object.prototype.hasOwnProperty.call(b, attr)) {
c[attr] = b[attr];
@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)
@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 / 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 / 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);