Skip to content

Instantly share code, notes, and snippets.

@cgray
cgray / gist:5633077
Last active July 19, 2018 07:55
[PHP] Convert Roman Numerals to Decimal
// from http://stackoverflow.com/questions/6265596/how-to-convert-a-roman-numeral-to-integer-in-php
$romans = array(
'M' => 1000,
'CM' => 900,
'D' => 500,
'CD' => 400,
'C' => 100,
'XC' => 90,
'L' => 50,
<?php
function array_get_value($array, $index, $default = null){
return array_key_exists($index, $array)?$array[$index]:$default;
}
?>
<?php
$subject = <<<DATA
property=value property2="value2" property3='value3'
DATA;
$pattern = '(
(?:^|\s)
(?<name>[a-z]\w+)
(?:=)
@cgray
cgray / 7bitparitycheck.php
Created August 18, 2015 06:25
7 bit parity check in php
$bitsOn = ($parity = $num & 1) + !!($num & 2) + !!($num & 4) + !!($num & 8) + !!($num & 16) + !!($num & 32) + !!($num & 64);
@cgray
cgray / route-to-url.php
Created April 4, 2017 03:54
Given an array of placeholder values and a FastRoute uri definition build a url
<?php
function buildUrl($uri, array $params) {
$matches = [];
$pattern = '{\s* ([a-zA-Z_][a-zA-Z0-9_-]*) \s*(?:: \s* ([^{}]*(?:\{(?-1)\}[^{}]*)*))?\}';
$optionalSegments = '\[.*'.$pattern.'\]';
$uri = preg_replace_callback('~'.$optionalSegments.'~x', function($match) use ($params) {
$uri = trim($match[0], '[]');
if (isset($params[$match[1]])) {
@cgray
cgray / trytoprogram3-edited.py
Last active February 3, 2020 01:11
Brittany Homework
#Brittany Schaefer
#CSC 115 2-01-2020
#Program 3
#Intro
print('Welcome to iCalculator')
#accumulators
add = 0
sub = 0