Skip to content

Instantly share code, notes, and snippets.

@jkoop
jkoop / screen.php
Last active August 29, 2021 00:17
php8.0 - print text to arbitrary place on the terminal with colour
<?php
class Screen {
public function __construct() {
$this->clear();
$this->fgColour = 'white';
$this->bgColour = 'black';
}
public function clear(): void {
@jkoop
jkoop / curl.php
Last active December 25, 2021 00:55
php7.4 - helper function for curl
<?php
// https://gist.github.com/jkoop/a3c0e6bb7572f58b70fd5f7511b70e8f
/**
* `return->body->json` => `json_decode(return->body->dump)`
*
* @param string $url URL will not be url-encoded
* @param array $fields
* @param string $method HTTP method to use; defaults to GET
@jkoop
jkoop / numberToMetric.php
Last active August 12, 2021 21:35 — forked from liunian/gist:9338301
php8.0 - number to metric with prefix for things like human readable file size
<?php
function numberToMetric(int|float $number, int $significantDigits = 3, bool $binary = false): string {
static $prefixes = ['y','z','a','f','p','n','μ','m','','k','M','G','T','P','E','Z','Y'];
$factor = floor((strlen(abs($number)) - 1) / 3) * (abs($number) <=> 1);
$metricBase = $binary ? 1024 : 1000;
$number = $number / pow($metricBase, $factor);
return sprintf("%.{$significantDigits}H", $number) . $prefixes[$factor+8];