Skip to content

Instantly share code, notes, and snippets.

View Mulkave's full-sized avatar
⌨️
binge coding

Abed Halawi Mulkave

⌨️
binge coding
View GitHub Profile
@Mulkave
Mulkave / UUIDv4.php
Last active September 20, 2015 01:49
UUID v4 implementation
<?php
// @see http://php.net/manual/en/function.uniqid.php
function v4() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
// 16 bits for "time_mid"
mt_rand(0, 0xffff),
@Mulkave
Mulkave / AllArrayItemsCombinations.php
Created August 16, 2015 11:39
Get all the possible combinations between the items of the given array
<?php
function allCombinationsForPossibilities(array $possibilities)
{
// initialize by adding the empty set
$results = array(array( ));
foreach ($possibilities as $element) {
foreach ($results as $combination) {
$results[] = array_merge(array($element), $combination);
@Mulkave
Mulkave / PHP Combinations & Cartesian.php
Last active August 29, 2015 14:27
Find all the possible combinations of the given associative array(s)
<?php
// http://stackoverflow.com/a/8567199/1542779
function combinations($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return array();
}
if ($i == count($arrays) - 1) {
return $arrays[$i];

Nginx FastCGI response buffer sizes

By default when Nginx starts receiving a response from a FastCGI backend (such as PHP-FPM) it will buffer the response in memory before delivering it to the client. Any response larger than the set buffer size is saved to a temporary file on disk. This process is also explained at the Nginx ngx_http_fastcgi_module page document page.

Since disk is slow and memory is fast the aim is to get as many FastCGI responses passing through memory only. On the flip side we don't want to set an excessively large buffer as they are created and sized on a per request basis (it's not shared).

The related Nginx options are:

@Mulkave
Mulkave / mb_parse_url.php
Created February 27, 2015 10:15
UTF-8 aware parse_url() replacement
// Courtesy of http://php.net/manual/en/function.parse-url.php#114817
/**
* UTF-8 aware parse_url() replacement.
*
* @return array
*/
function mb_parse_url($url)
{
$enc_url = preg_replace_callback(
-- gets all fields from a hash as a dictionary
local hgetall = function (key)
local bulk = redis.call('HGETALL', key)
local result = {}
local nextkey
for i, v in ipairs(bulk) do
if i % 2 == 1 then
nextkey = v
else
result[nextkey] = v

Share Counts

I have always struggled with getting all the various share buttons from Facebook, Twitter, Google Plus, Pinterest, etc to align correctly and to not look like a tacky explosion of buttons. Seeing a number of sites rolling their own share buttons with counts, for example The Next Web I decided to look into the various APIs on how to simply return the share count.

If you want to roll up all of these into a single jQuery plugin check out Sharrre

Many of these API calls and methods are undocumented, so anticipate that they will change in the future. Also, if you are planning on rolling these out across a site I would recommend creating a simple endpoint that periodically caches results from all of the APIs so that you are not overloading the services will requests.

Twitter

# A class-based template for jQuery plugins in Coffeescript
#
# $('.target').myPlugin({ paramA: 'not-foo' });
# $('.target').myPlugin('myMethod', 'Hello, world');
#
# Check out Alan Hogan's original jQuery plugin template:
# https://github.com/alanhogan/Coffeescript-jQuery-Plugin-Template
#
(($, window) ->
{
"auto_complete": true,
"caret_style": "phase",
"ensure_newline_at_eof_on_save": true,
"file_exclude_patterns":
[
"vendor/*",
"node_modules/*",
"!vendor/*.coffee",
"!vendor/*.js"
@Mulkave
Mulkave / .vimrc
Last active August 29, 2015 14:03
My way to VIM from Sublime Text - .vimrc
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, required