Skip to content

Instantly share code, notes, and snippets.

@jrivero
jrivero / FizzBuzz.php
Last active August 29, 2015 13:57
FizzBuzz implementation
<?php
function FizzBuzz($from=1, $to=100)
{
$result = [];
for($i = $from; $i <= $to; $i++) {
if ($i % 15 == 0) {
$result[] = 'FizzBuzz';
} elseif ($i % 3 == 0) {
#!/bin/sh
wget -N http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gzip -d -f GeoIP.dat.gz
wget -N http://geolite.maxmind.com/download/geoip/database/GeoIPv6.dat.gz
gzip -d -f GeoIPv6.dat.gz
wget -N http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gzip -d -f GeoLiteCity.dat.gz
<?php
function array_group_by(array $array, callable $key_selector)
{
$result = [];
foreach ($array as $values) {
$key = call_user_func($key_selector, $values);
$result[$key][] = $values;
}
@jrivero
jrivero / file_get_contents_curl.php
Last active February 21, 2024 11:30
Alternative for file_get_contents() using curl
<?php
// http://25labs.com/alternative-for-file_get_contents-using-curl/
function file_get_contents_curl($url, $retries=5)
{
$ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36';
if (extension_loaded('curl') === true)
{
@jrivero
jrivero / JsonHandler.php
Created May 7, 2013 13:09
Handling JSON like a boss in PHP
<?php
// http://nitschinger.at/Handling-JSON-like-a-boss-in-PHP
class JsonHandler
{
protected static $_messages = array(
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
@jrivero
jrivero / var_dump.php
Created May 7, 2013 13:04
My personal minimalistic var_dump function
<?php
function vd($var, $return=false)
{
$s = var_export($var, true);
$s = preg_replace( '/[^[:print:]]/', '',$s);
$s = preg_replace('#[ ]+#', '', $s);
$s = str_replace(array('array(', ')', '=>', ',]'), array('[', ']', ':', ']'), $s);
if ($return) return $s;
@jrivero
jrivero / konami.js
Created April 2, 2013 10:42
Konami code with jquery
$(document).ready(function(){
var keys = [];
var konami = '38,38,40,40,37,39,37,39,66,65';
$(document)
.keydown(
function(e) {
keys.push( e.keyCode );
if ( keys.toString().indexOf( konami ) >= 0 ){
console.log("konami code")
@jrivero
jrivero / cachedHTMLForURL.php
Created September 24, 2012 11:12
Retrieve Google Cached HTML
<?php
// http://hactheplanet.com/blog/11
function cachedHTMLForURL($url)
{
// Request the cache from Google.
$googleRequestURL = "http://webcache.googleusercontent.com/search?q=" . urlencode("cache:" . $url);
$googleResponse = file_get_contents($googleRequestURL);
@jrivero
jrivero / hack.sh
Created March 31, 2012 13:32 — forked from erikh/hack.sh
OSX For Hackers
#!/usr/bin/env sh
##
# This is script with usefull tips taken from:
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
#
# install it:
# curl -sL https://raw.github.com/gist/2108403/hack.sh | sh
#
@jrivero
jrivero / make_pretty.php
Last active February 21, 2024 11:34
Human redable sizes
<?php
// http://www.geekality.net/2010/11/17/php-output-a-number-of-bytes-in-a-human-readable-way/
function make_pretty($bytes)
{
$symbols = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
$exp = floor(log($bytes) / log(1024));
return sprintf('%.2f '.$symbols[$exp], $bytes/pow(1024, floor($exp)));