Skip to content

Instantly share code, notes, and snippets.

@tored
tored / bcmath.php
Last active January 11, 2022 13:44
PHP bcceil, bcfloor and bcround for bcmath
<?php
declare(strict_types=1);
// https://stackoverflow.com/a/1653826
// updated for PHP 8
function bcceil(string $number): string
{
if (str_contains($number, '.')) {
if (preg_match("~\.[0]+$~", $number)) {
@tored
tored / getOptions.php
Last active September 30, 2021 17:33
getOptions: experimental command line parser for PHP by using attributes
<?php
declare(strict_types=1);
#[Attribute(Attribute::TARGET_FUNCTION | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class Option
{
public function __construct(
public string|null $short = null,
public string|null $long = null,
public bool $required = false,
@tored
tored / asciiTable.php
Last active August 11, 2021 09:18
asciiTable
<?php
declare(strict_types=1);
function asciiTable(array $rows, array $columns, ?callable $mapper = null): string
{
if ($mapper === null) {
$mapper = function (string $column, $value) {
return $value;
};
}
@tored
tored / php.cmd
Last active May 2, 2020 15:26
Configurable execution of PHP scripts in Windows
@echo off
if "%PHP%"=="" (
echo %%PHP%% is NOT defined
exit /b
)
%PHP% %*
@tored
tored / phps.php
Last active April 30, 2020 11:03
Auto-download composer dependencies for php script
#!/usr/bin/env php
<?php
declare(strict_types=1);
const PHPS = 'phps';
const COMMENT = '#';
const BANG = '!';
if (empty($argv[1])) {
throw new RuntimeException("Missing php file");
@tored
tored / msn.ps1
Created June 21, 2017 11:34
MSN block
New-NetFirewallRule -DisplayName "Block Microsoft Edge News Feed by IP" -RemoteAddress "204.79.197.203" -Action "Block" -Direction "Outbound" -Package "S-1-15-2-3624051433-2125758914-1423191267-1740899205-1073925389-3782572162-737981194"
@tored
tored / fork.php
Last active September 20, 2016 08:55
fork - simple fork for both Windows (with PowerShell) and Un*x (with Bash) in PHP. Returns PID.
<?php
function fork($program, ...$args): int
{
if (strtoupper(php_uname('s')) === 'WINDOWS NT') {
$cmd = [
'powershell.exe',
'-Command',
'$proc',
'=',
@tored
tored / bing.php
Last active June 17, 2016 09:36
Bing Wallpaper
#!/usr/bin/env php
<?php
function getDownloadPathWindows()
{
$home = getenv('HOMEDRIVE') . getenv('HOMEPATH');
if (!is_dir($home)) {
throw new RuntimeException("No home folder $home");
}
return "$home/Pictures/Bing";
@tored
tored / .nanorc
Last active July 13, 2016 19:19
nanorc
set autoident
set tabsize 4
set tabstospaces
set multibuffer
set smarthome
set whitespace "»·"
set const
set historylog
set mouse
set poslog
@tored
tored / move.sh
Last active October 26, 2015 14:36
Move files between git repos keeping history
# http://stackoverflow.com/a/11426261
cd repository
git log --pretty=email --patch-with-stat --reverse --full-index --binary -- path/to/file_or_folder > patch
cd ../another_repository
git am < ../repository/patch