Skip to content

Instantly share code, notes, and snippets.

@RyanNutt
RyanNutt / preg_search
Last active August 29, 2015 14:04
One liner to get RegEx matches in PHP
@RyanNutt
RyanNutt / Rotate a string
Created September 28, 2014 15:45
I needed an easy way to rotate a string a set number of places for a lab I was setting up. This returns s rotated by distance spots and loops back around.
private String rotateString(String s, int distance) {
String out = "";
for (int i=0; i<s.length(); i++) {
out += s.charAt((i + distance) % s.length());
}
return out;
}
@RyanNutt
RyanNutt / moodle-quiz-count.sql
Last active September 24, 2015 20:26
MySQL query for ad-hoc Moodle queries to count quizzes - http://www.nutt.net/2015/01/22/pulling-grades-moodle-quizzes/
SELECT u.lastname, u.firstname, u.idnumber,
(
SELECT COUNT(*)
FROM prefix_quiz_attempts attempts
JOIN prefix_quiz quiz
ON attempts.quiz=quiz.id
JOIN prefix_course_modules cm
ON cm.instance=quiz.id
WHERE
attempts.userid=u.id
@RyanNutt
RyanNutt / del_recursively
Last active September 27, 2015 19:53
Delete all files by extension recursively. This one was to delete all flac files out of a set of folders after converting to mp3.
del /S *.flac
@RyanNutt
RyanNutt / git.archive.sh
Created September 24, 2015 23:41
Git command to export a repository into a zip file with prefix. Can use this to create zip files from GitLab archives maybe???
git archive --format=zip --prefix=foldername/ > zipfile.zip
@RyanNutt
RyanNutt / base64_maybe.php
Created October 3, 2015 15:58
Function to base64 decode a string if it appears to be base64 encoded, or returns the string unchanged if it doesn't look like it's encoded.
function base64_decode_maybe($str) {
if (base64_encode(base64_decode($str, true)) == $str) {
return base64_decode($str);
}
return $str;
}
@RyanNutt
RyanNutt / hideGutter.js
Created October 4, 2015 14:08
Hide line number gutter from Ace editor
var edit = ace.edit('editorDiv');
edit.renderer.setShowGutter(false);
@RyanNutt
RyanNutt / paramerterized_test.java
Last active October 4, 2015 19:52
I use a lot of parameterized JUnit tests in class and got tired of copying and pasting an existing full test file and then deleting the pieces I don't need. This one is pretty much cut down to the minimum.
import java.lang.reflect.Field;
import java.util.*;
import java.io.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
@RyanNutt
RyanNutt / WebSafe.php
Last active November 27, 2015 13:39
PHP script to return the hex codes of the 216 "web safe" colors
<?php
function web_safe_colors() {
$ray = array();
$steps = array(
'00',
'33',
'66',
'99',
'cc',
'ff' );