Skip to content

Instantly share code, notes, and snippets.

View bdelespierre's full-sized avatar
🚩
Working from home

Benjamin Delespierre bdelespierre

🚩
Working from home
View GitHub Profile
<?php
function formatGUID($chars, $uppercase = false, $braces = false, $hyphen = true)
{
$c = $chars;
$h = $hyphen ? '-' : '';
$g =
"{$c[00]}{$c[01]}{$c[02]}{$c[03]}{$c[04]}{$c[05]}{$c[06]}{$c[07]}{$h}".
"{$c[08]}{$c[09]}{$c[10]}{$c[11]}{$h}".
"{$c[12]}{$c[13]}{$c[14]}{$c[15]}{$h}".
@bdelespierre
bdelespierre / hamming_dist_keywords
Created December 17, 2014 14:58
Keywords hamming distance
<?php
$campaigns = [
'foo' => ['keywords' => ['a','b','c']],
'bar' => ['keywords' => ['b','c','e']],
'baz' => ['keywords' => ['d','f','b']],
];
$allKeywords = array_unique(call_user_func_array('array_merge', array_map(function($c) { return $c['keywords']; }, $campaigns)));
<?php
// cet algorithme est d'une telle violence
$dim = 3;
$d = 32; // lol
$v1 = array_randn($dim);32;
$v2 = array_randn($dim);
CREATE TABLE IF NOT EXISTS _EL_INFORME_DE_LA_MUERTE (
CAMPAIGNID INT UNSIGNED NOT NULL,
ADASSETID INT UNSIGNED NOT NULL,
COUNTRY_CODE CHAR(2) NOT NULL DEFAULT '',
CITY_CODE VARCHAR(50) NOT NULL DEFAULT '',
TYPE ENUM('DISPLAY','CLICK','SALE','POST_VIEW','POST_CLICK'),
AMOUNT INT UNSIGNED NOT NULL DEFAULT 0,
DATE_SAVE DATE,
PRIMARY KEY (CAMPAIGNID, ADASSETID, TYPE, DATE_SAVE)
) ENGINE=MyISAM;
<?php
class Bitfield
{
const FIELD_SIZE = 32;
public static function getDistance(array $a, array $b)
{
$dist = 0;
@bdelespierre
bdelespierre / AdvancedNewFile.sublime-settings
Created September 24, 2015 15:57
AdvancedNewFile.sublime-settings
{
"completion_type": "nix",
"folder_permissions": "775",
"file_permissions": "775"
}
@bdelespierre
bdelespierre / phpbug#54547_workaround
Created May 9, 2012 09:53
PHP Bug #54547 Workaround
// Case 1
// will display '9223372036854775807 equals 9223372036854775808'
// bug https://bugs.php.net/bug.php?id=54547
$a = '9223372036854775807';
$b = '9223372036854775808';
if ($a == $b)
echo "$a equals $b\n";
@bdelespierre
bdelespierre / md5.php
Last active December 12, 2015 00:29
Get the md5 of your whole PHP application
<?php
function glob_recursive($pattern, $flags = 0) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
}
return $files;
}
@bdelespierre
bdelespierre / lines.php
Last active December 12, 2015 01:38
Ever wondered how many lines you've written in your PHP project ? This is almost the most useless script in history but I'm kinda curious so I wrote it anyway...
<?php
set_time_limit(0);
function glob_recursive($pattern, $flags = 0) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
}
return $files;
}
@bdelespierre
bdelespierre / singleton.php
Created March 14, 2013 16:57
Singleton (boo) + Adapter (yay)
<?php
class Singleton {
public static $aliases = array();
protected static $_adapters = array();
public function __construct () {
throw new LogicException("you cannot instanciate Singleton");