Skip to content

Instantly share code, notes, and snippets.

@polonskiy
polonskiy / fork.php
Last active August 29, 2015 14:07
echo servers in php and go
<?php
$server = stream_socket_server('tcp://127.0.0.1:8000');
while (true) {
$client = stream_socket_accept($server, -1);
if (pcntl_fork() === 0) {
stream_copy_to_stream($client, $client);
die;
}
fclose($client);
#!/usr/bin/php
<?php
$data = file_get_contents('php://stdin');
preg_match('#^To:(.*)#i', $data, $matches);
$filename = tempnam('/tmp', trim($matches[1]) . '_');
file_put_contents($filename, $data);
@polonskiy
polonskiy / init.sh
Created February 12, 2015 14:33
forward signals
#!/bin/bash
#enable job control
set -m
#propagate INT/TERM signals
trap 'kill -TERM $(jobs -p); wait' TERM
trap 'kill -INT $(jobs -p); wait' INT
#services
@polonskiy
polonskiy / float-comma.php
Last active August 29, 2015 14:16
comma deciaml separator
<?php
setlocale(LC_ALL, 'uk_UA.UTF-8');
$float = 1.2345;
echo "$float\n";
@polonskiy
polonskiy / csv-decode.php
Last active August 29, 2015 14:23
csv decode
<?php
function csv_decode($csv, $columns = [], $delimiter = ',', $enclosure = '"', $escape = '\\') {
$tmp = fopen('php://temp', 'rb+');
fwrite($tmp, $csv);
rewind($tmp);
$result = [];
$assoc = (bool) $columns;
@polonskiy
polonskiy / git-del.sh
Created September 22, 2015 11:22
remove file from git
#!/bin/bash
git filter-branch --prune-empty --index-filter "git rm --cached -f --ignore-unmatch $1" --tag-name-filter cat -- --all
@polonskiy
polonskiy / gist:6290981
Created August 21, 2013 06:33
get all memcached keys and values
echo 'stats items' | nc localhost 11211 | while read line; do echo $line | grep -Po 'STAT items:\d+' | grep -Po '\d+'; done | sort -u | xargs -L1 -i echo 'stats cachedump {} 100' | nc localhost 11211 | grep 'ITEM' | sed 's/ITEM \([^ ]\+\) .*/\1/' | xargs -L1 -i echo 'get {}' | nc localhost 11211
<?php
error_reporting(E_ALL);
class O_o
{
public $wth,$mode,$num;
function __construct()
{
$this->wth = "";
$this->mode;

Microframework

This is a PHP (5.3+) microframework based on anonymous functions.

Features

  • requested URLs matched using regular expressions
  • request methods (matches using regular expressions too)
  • differenced FIFO queues for each $priority
  • command line usage
  • backward compatibility
  • integrated Dependency Injection and settings system
  • named patterns

140 byte PHP routing system.

This is a very simply routing system that makes it easy to test requests to different paths. This is very limited so do not use for your applications - it's just for fun.

require('route.php');

// A user profile
route('/(\w+)/profile', function($path, $user)
{

print "Hello " . $user;