Skip to content

Instantly share code, notes, and snippets.

@OO00O0O
OO00O0O / router.php
Created Oct 17, 2021
Simple regexp router
View router.php
<?php declare(strict_types=1);
$routes = [
['GET /(?<something_file>[a-z][a-z0-9]{8}\.[a-z0-9]{3,4})', '...meta...'],
['GET /(?<ok>what|na)', '...meta...'],
['POST /', '...meta...'],
];
$regexp = implode('|', array_map( // could be cached
static function (int $index, array $values) {
@OO00O0O
OO00O0O / di.php
Last active Jan 28, 2021
PHP single function dependency injection
View di.php
<?php declare(strict_types=1);
if (PHP_VERSION_ID < 80000) { die('only php 8'); }
/**
* Single function dependency injection POC
*/
interface Inter {}
class A { public function __construct(private int $id) {} }
class E { public function __construct(private int $id) {} }
class B { public function __construct(private A|E $a) {} }
View alphaID.php
<?php
/**
* Translates a number to a short alhanumeric version
*
* Translated any number up to 9007199254740992
* to a shorter version in letters e.g.:
* 9007199254740989 --> PpQXn7COf
*
* specifiying the second argument true, it will
* translate back e.g.:
View gist:9e70c63c6a96a10230441b75115a3303
<?php
/**
* Lietuviškų vardų linksniai.
*
* @author Maug Lee <mauglee@gmail.com>
* @copyright Copyleft (ↄ) 2011, Maug Lee
* @version 0.3
* @package Vardai
*/
@OO00O0O
OO00O0O / translit.php
Last active May 18, 2016
Multi lang slug generator with simple translit
View translit.php
<?php
public static function slug($str, $replace = [], $delimiter = '-')
{
if(count($replace)) {
$str = str_replace($replace, ' ', $str);
}
setlocale(LC_CTYPE, 'en_US.UTF8');
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));