Skip to content

Instantly share code, notes, and snippets.

@tom--
tom-- / slug.php
Last active November 25, 2016 17:08
Make a slug for an earl in PHP
<?php
function slug(string $string, int $width = 100) : string
{
$string = Normalizer::normalize($string);
$string = trim(preg_replace('/[^\pL\p{Nd}]+/u', ' ', $string));
$string = strtr($string, ' ', '-');
return mb_strimwidth($string, 0, $width, '', 'UTF-8');
@tom--
tom-- / Persister.php
Last active November 29, 2016 18:24
Conceptual demo of data mapping in a Yii webapp. It's no prototype to elaborate into real software. It's only an object of philosophical contemplation.
<?php
/**
* A global Persister "static" class isolates the location of any persistence logic from its users.
* In this example, persitence logic for UserMovieRating objects is located in this class but it
* could be moved around ad hoc.
*/
class Persister
{
private static function _UserMovieRatingSave(UserMovieRating $object)
@tom--
tom-- / mt_rand() PHP 7.1 statistical tests.md
Last active July 27, 2016 15:59
Statistical testing of updated mt_rand() in PHP 7.1 using PractRand and Dieharder
---
- name: Add APT repo key
apt_key:
url: "https://download.newrelic.com/548C16BF.gpg"
id: "548C16BF"
state: "present"
- name: Add APT repo
apt_repository:
<?php
$a = [1, 3];
foreach ($a as $v) {
var_dump($v);
$a[1] = 9;
}
@tom--
tom-- / TestU01_BigCrush_results.txt
Last active June 17, 2016 00:01
Tests of PHP mt_rand()
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Starting BigCrush Starting BigCrush
Version: TestU01 1.2.3 Version: TestU01 1.2.3
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*********************************************************** ***********************************************************
Test smarsa_SerialOver calling smultin_MultinomialOver Test smarsa_SerialOver calling smultin_MultinomialOver
*********************************************************** ***********************************************************
@tom--
tom-- / Random bytes, ints, UUIDs in PHP.md
Last active February 11, 2023 06:14
PHP random bytes, integers and UUIDs

Random bytes, ints, UUIDs in PHP

Simple and safe random getters to copy-paste

string randomBytes( int $length )

int randomInt ( int $min , int $max )

string randomUuid ( void )
@tom--
tom-- / generateRandomKeyReqs.php
Created January 4, 2016 16:28
Detailed requirements checks for Yii 2.0.7's yii\base\Security::generateRandomKey()
<?php
$tests = [
"function_exists('random_bytes')",
"defined('OPENSSL_VERSION_TEXT') ? OPENSSL_VERSION_TEXT : null",
"PHP_VERSION_ID",
"function_exists('mcrypt_create_iv') ? bin2hex(mcrypt_create_iv(8, MCRYPT_DEV_URANDOM)) : null",
"DIRECTORY_SEPARATOR",
"sprintf('%o', lstat('/dev/urandom')['mode'])",
"sprintf('%o', lstat('/dev/urandom')['mode'] & 0170000)",
@tom--
tom-- / Security.php
Created January 4, 2016 16:24
Extends yii\base\Security::generateRandomKey() to use suspicious OpenSSL setups
<?php
namespace yourapp;
use yii\helpers\StringHelper;
class Security extends \yii\base\Security
{
/**
* Extends yii\base\Security::generateRandomKey() to use suspicious OpenSSL setups if
@tom--
tom-- / nullable_return.wiki
Last active April 14, 2016 15:05
Something|null return type declaration
PHP RFC: Nullable Return Type Declaration

  * Version: 0.1
  * Date: Apr 14 2016
  * Author: Tom Worster, fsb@thefsb.org
  * Status: Under Discussion
  * First Published at: https://wiki.php.net/rfc/nullable_return

Introduction