Skip to content

Instantly share code, notes, and snippets.

View amnuts's full-sized avatar

Andrew Collington amnuts

View GitHub Profile
@amnuts
amnuts / scraping.php
Last active April 21, 2021 06:44
Example of how to scrape multiple pages using Zend\Dom from Zend Framework 2.
<?php
use \Zend\Dom\Query;
use \Zend\Debug\Debug;
/**
* Fetch the page source and cache it, ensuring it's saved as UTF-8
*
* @param string $url
* @return string
@amnuts
amnuts / merge_urls.php
Last active March 11, 2021 14:39
Merge two urls using PHP
<?php
/**
* Combine two urls.
*
* The urls can be either a string or url parts that consist of:
*
* scheme, host, port, user, pass, path, query, fragment
*
* If passed in as parts in an array, the query parameter can be either
@amnuts
amnuts / stuff.php
Last active March 10, 2021 11:59
jotting down functions
<?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 / ofilter.php
Last active January 11, 2021 08:44
Filter an array of objects based on property, or multiple property values
<?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 / propSearch.js
Created October 6, 2020 14:59
Helpers to see if object has all props or any of the props
const hasAllProps = function(list, within) {
list.forEach(function(e) {
if (!within.hasOwnProperty(e) || within[e] == "") {
return false;
}
});
return true;
};
const hasAnyProps = function(list, within) {
@amnuts
amnuts / osort.php
Last active June 8, 2020 20:35
Sort array of objects by one or more properties of the object.
<?php
/**
* Sort an array of objects.
*
* Requires PHP 5.3+ to be installed.
*
* Will use the Intl extension to normalize characters if it's
* available.
*
@amnuts
amnuts / speechSynthesis.html
Last active March 13, 2020 12:58
Add a link to read text aloud including the option of swapping to any available voices.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script
src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
@amnuts
amnuts / directory-reflection.php
Created January 6, 2020 18:04
Using Better Reflection to get details on classes in multiple directories
<?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 June 4, 2019 15:57
PHP date/time conversion example
<?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 / timer.php
Last active October 26, 2018 12:26
Simple PHP timer class
<?php
/**
* Utility class to enable very simplistic timing.
*
* Usage example:
*
* $t = new Timer();
* // do something here
* echo $t;