Skip to content

Instantly share code, notes, and snippets.

Avatar

Andrew Collington amnuts

View GitHub Profile
@amnuts
amnuts / directory-reflection.php
Created Jan 6, 2020
Using Better Reflection to get details on classes in multiple directories
View directory-reflection.php
<?php
use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\SourceLocator\Type\DirectoriesSourceLocator;
require __DIR__ . '/vendor/autoload.php';
$refSubscriptions = [];
@amnuts
amnuts / datetime.php
Created Jun 4, 2019
PHP date/time conversion example
View datetime.php
<?php
foreach (range(1, 12) as $m) {
$elStart = new \DateTimeImmutable("2019-{$m}-10 23:50:00", new \DateTimeZone('Europe/London'));
$elEnd = $elStart->add(new \DateInterval('PT5H'));
$utcStart = $elStart->setTimezone(new \DateTimeZone('UTC'));
$utcEnd = $elEnd->setTimezone(new \DateTimeZone('UTC'));
printf("E/L: %s / %s, UTC: %s / %s\n",
$elStart->format(DATE_RFC822),
$elEnd->format(DATE_RFC822),
@amnuts
amnuts / stuff.php
Last active Mar 22, 2019
jotting down functions
View stuff.php
<?php
// Return greatest common divider of two numbers
function gcd($a, $b) {
return $b ? gcd($b, $a % $b) : $a;
}
// Returns the least common multiple of two or more numbers.
function lcm(...$numbers): int
{
@amnuts
amnuts / timer.php
Last active Oct 26, 2018
Simple PHP timer class
View timer.php
<?php
/**
* Utility class to enable very simplistic timing.
*
* Usage example:
*
* $t = new Timer();
* // do something here
* echo $t;
@amnuts
amnuts / limit-textarea.js
Created Sep 18, 2017
Show word or character count on text boxes/areas that have data-max-length="<number>" and data-max-type="(character|word)" attributes.
View limit-textarea.js
<script type="text/javascript">
$(function(){
function wordCount(str, charlist) {
var len = str.length, cl = charlist && charlist.length,
chr = '', tmpStr = '', i = 0, c = '', wC = 0, reg = '', match = false;
var _preg_quote = function(str) {
return (str + '').replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!<>\|\:])/g, '\\$1');
}
var _getWholeChar = function(str, i) {
var code = str.charCodeAt(i);
@amnuts
amnuts / sort.js
Created Jun 14, 2017
Sort js hash, retaining key association
View sort.js
// From: https://stackoverflow.com/a/36079484
// The unsorted data
let data = {
a: 'A',
c: 'C',
b: 'B'
};
// Create it sorted
@amnuts
amnuts / ofilter.php
Last active Jul 10, 2020
Filter an array of objects based on property, or multiple property values
View ofilter.php
<?php
/**
* Filter an array of objects.
*
* You can pass in one or more properties on which to filter.
*
* If the key of an array is an array, then it will filtered down to that
* level of node.
*
@amnuts
amnuts / gist:d366e76122f1ad32052d8b82dd2d516c
Created Apr 6, 2017
Convert lots of h264 files to mp4 from command line on rpi
View gist:d366e76122f1ad32052d8b82dd2d516c
# first make sure MP4Box is install
sudo apt-get install gpac
# then run this from the command line
for i in *.h264; do MP4Box -add $i ${i%.*}.mp4; done
@amnuts
amnuts / volume.py
Created Jan 11, 2017
Use a rotary controller to adjust volume on a Raspberry Pi
View volume.py
from RPi import GPIO
from time import sleep
import subprocess
clk = 5
dt = 6
btn = 26
# vals from output of amixer cget numid=1
min = 0
@amnuts
amnuts / .bashrc
Last active Mar 17, 2016
Bash prompt stuff
View .bashrc
# change the colour of the base directory depending on whether
# I'm in the live/staging/dev areas. Also show the user and host
# as well as the bash history number of the command.
C_DEV="\[\033[0;32m\]" # green
C_STAGE="\[\033[0;33m\]" # yellow
C_LIVE="\[\033[1;31m\]" # bold red
C_PATH="\[\033[0;37m\]" # white
C_COUNT="\[\033[0;36m\]" # cyan
C_NONE="\[\033[0m\]" # reset
You can’t perform that action at this time.