Skip to content

Instantly share code, notes, and snippets.

View gowon's full-sized avatar

Gowon Patterson gowon

View GitHub Profile
@gowon
gowon / gibberish.php
Last active December 22, 2015 18:09
Quick and dirty lipsum-type generator. Throws random letters together to create paragraphs.
<?php
function gibberish($numParagraphs = 1, $wordsPerParagragh = 10, $maxWordLength = 20)
{
mt_srand(microtime() * 1000000);
$bank = array_merge(range('a', 'z'), range('A', 'Z'));
$paragraphs = array();
for ($i = 0; $i < $numParagraphs; $i++)
{
$words = array();
@gowon
gowon / simplexmlencoder.php
Last active August 29, 2015 13:57
Simple XML Encoder
<?php
function simple_xml_encoder($data, $rootNode = 'response', $node = 'string') {
function traverse($data, $node) {
$xml = '';
if (is_array($data) || is_object($data)) {
foreach ($data as $key => $value) {
if (is_numeric($key)) {
$key = $node;
}
if (empty($value)) {
@gowon
gowon / include-into-variable.php
Last active August 29, 2015 13:57
Storing data into variable on include/require
<?php
//include.php
function add($x, $y) {
return $x + $y;
}
return add(5,10);
?>
@gowon
gowon / log.js
Last active August 29, 2015 13:57
Wrapper for console.log
//TODO: http://stackoverflow.com/questions/7042611/override-console-log-for-production
;
(function (window) {
if (window.log) {
return;
}
window.log = function() {
log.history = log.history || []; // store logs to an array for reference
log.history.push(arguments);
@gowon
gowon / ParseUtility.cs
Created April 23, 2014 15:55
String object extension to convert into other object types
using System;
using System.Collections.Generic;
using System.Reflection;
namespace ParseUtility
{
public static class ParseUtility
{
//-- Parsing
@gowon
gowon / coalesce.cs
Last active August 29, 2015 14:01
Simple coalesce extension method for strings. Behaves similarly to the "??" null coalesce operator, but will properly collapse in situations of empty/whitespace strings.
public static class StringCoalesceExtension
{
public enum CoalesceProperty
{
OnNull = 1,
OnNullOrEmpty = 2,
OnNullOrWhiteSpace = 4
}
public static string Coalesce(this string originalString, params string[] strings)
@gowon
gowon / bytes.php
Created May 24, 2014 04:23
Simple Byte-size handling class
<?php
class Bytes
{
private $sizeInBytes;
private static $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
public function __construct($sizeInBytes)
{
$this->sizeInBytes = floatval($sizeInBytes);
@gowon
gowon / db2-aggregate-string.sql
Created June 6, 2014 20:20
Aggregating Strings in DB2
-- Based on https://www.ibm.com/developerworks/community/blogs/SQLTips4DB2LUW/entry/aggregating_strings42?lang=en
SELECT
dept,
SUBSTR(XMLCAST(XMLGROUP(',' || name AS a
ORDER BY name)
AS VARCHAR(60)), 2) AS Names,
avg(salary) as avgsal
FROM emp
GROUP BY dept;
@gowon
gowon / ob_minify.php
Last active August 29, 2015 14:03
Minify PHP Output Buffer - Compress HTML output to a single line, then attempt gzip compression
<?php
/**
* Smart Tidy/Gzip Output Buffer
* Compress HTML output to a single line, then attempt gzip compression.
* @param string $buffer
* @param int $mode
* @return string|false
*/
function ob_minify($buffer, $mode) {
@gowon
gowon / autoload.php
Last active September 13, 2019 08:16
Directory-based autoload function
<?php
/* Directories that contain classes */
$classesDir = array (
ROOT_DIR.'classes/',
ROOT_DIR.'firephp/',
ROOT_DIR.'includes/'
);
function __autoload($class_name) {
global $classesDir;