Skip to content

Instantly share code, notes, and snippets.

View uppfinnarjohnny's full-sized avatar

Johnny Karhinen uppfinnarjohnny

View GitHub Profile
<?php
function array_flatten(Array $array, Array $out = array()) {
foreach($array as $value)
is_array($value) ? $out = array_flatten($value, $out) : $out[] = $value;
return $out;
}
@uppfinnarjohnny
uppfinnarjohnny / MobWritePatcher.php
Created December 5, 2009 19:20
MobWritePatcher
<?php
function MobWritePatcher($text, $patch) {
$pointer = 0;
foreach(explode("\t", $patch) as $step) {
$modifier = substr($step, 0, 1);
$argument = substr($step, 1);
switch($modifier) {
case '=':
<?php
class MobWrite {
protected $user = 0;
protected $file = NULL;
protected $datastore;
protected $diffengine;
public function __construct($datastore, $diffengine) {
$this->datastore = $datastore;
$this->diffengine = $diffengine;
@uppfinnarjohnny
uppfinnarjohnny / split_chunks.php
Created December 17, 2009 16:02
split_chunks
<?php
function split_chunks($input) {
$offset = 0;
foreach(array_slice(func_get_args(), 1) as $length) {
$out[] = substr($input, $offset, $length);
$offset += $length;
}
return $out;
}
@uppfinnarjohnny
uppfinnarjohnny / mod_querystring.php
Created January 21, 2010 19:04
mod_querystring()
<?php
function mod_querystring($name, $value = NULL) {
$vars = $_GET;
$vars[$name] = $value;
return http_build_query($name);
}
print http_build_query(array('hej'=>'tjo', 'hepp'=>'happ','haha' => NULL));
<?php
nth($what = 2, $loop = 'default') {
static $loops = array();
return ($loops[$loop] % $what == 0);
}
<?php
function swedish_date($date = NULL) {
if(is_null($date))
$date = time();
elseif( ! is_numeric($date))
$date = strtotime($date);
set_locale(LC_TIME, 'sv_SV');
return strftime('%A den %e %B %Y' , $date);
}
@uppfinnarjohnny
uppfinnarjohnny / caesar_cipher.php
Created December 23, 2010 13:20
A basic implementation of a Caesar cipher
<?php
function rollover($max, $value) {
return $value < $max ? $value : rollover($max, $value - $max);
}
function caesar_cipher($string, $offset = 3) {
$letter_numbers = array_map('ord', str_split(strtolower($string)));
foreach($letter_numbers as &$number)
if($number >= 97 && $number <= 122)
$number = rollover(25, $number - 97 + $offset) + 97;
<?php
function word_pairs($string, $min_matches = 1) {
$pair_counts = array();
$words = explode(' ', $string);
$previous_word = FALSE;
foreach($words as $word) {
if($previous_word)
$pair_counts[$previous_word.' '.$word]++;
$previous_word = $word;
}
@uppfinnarjohnny
uppfinnarjohnny / index.php
Created March 12, 2011 17:37
Tok-simpel MVC-struktur
<?php
include('posts_model.php');
$posts = get_posts();
include('view.php');