Skip to content

Instantly share code, notes, and snippets.

View Csardelacal's full-sized avatar

César de la Cal Csardelacal

View GitHub Profile
@Csardelacal
Csardelacal / uuid.php
Last active August 27, 2018 07:53
Generates valid UUID v4 in PHP.
<?php
function uuid() {
$data = random_bytes(16);
$data[6] = chr((ord($data[6]) | 0xC0) & 0x4F);
$data[8] = chr((ord($data[8]) | 0xC0) & 0xBF);
$str = bin2hex($str);
return sprintf('%s-%s-%s-%s-%s', substr($str, 0, 8), substr($str, 8, 4), substr($str, 12, 4), substr($str, 16, 4), substr($str, 20));
}
@Csardelacal
Csardelacal / Every.php
Last active January 18, 2016 14:54
Every
<?php
/**
* This class is intended as a tool to aid creating loops with special events in
* them. You can instance the class before running your loop and simply trigger
* the next() method whenever you need it.
*
* E.g. This is specially useful if you need to print a certain HTML tag every
* X executions of your loop. You can do it like this:
*
@Csardelacal
Csardelacal / array_filter_recursive.php
Created November 9, 2013 17:52
Filters an array recursively.
function array_filter_recursive($element) {
if ( is_array($element) ) {
foreach ($element as &$subelement) {
$subelement = self::array_filter_recursive($subelement);
}
return array_filter($element);
}
return !!$element;
}