Skip to content

Instantly share code, notes, and snippets.

@polonskiy
polonskiy / pre-commit
Last active March 20, 2020 07:39
PHPLint pre-commit git hook
#!/usr/bin/php
<?php
exec('git diff --name-only --diff-filter=ACM --cached', $files);
$status = 0;
foreach ($files as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) != 'php') continue;
$file = escapeshellarg($file);
$message = array();
exec("git show :$file | php -l 2>&1", $message, $code);
@polonskiy
polonskiy / get-teredo-addr.sh
Created January 2, 2014 06:34
get ipv6 (teredo) address
ip -6 -o addr | grep -P -o 2001:0:[^/]+
@polonskiy
polonskiy / pomodoro.sh
Last active January 2, 2016 02:38
pomodoro
#!/bin/bash
sleep ${1:-25}m
notify-send -i clock 'Time is up'
@polonskiy
polonskiy / byte2.php
Created January 9, 2014 13:30
Bytes to KB/MB/GB/TB converter
<?php
function byte2($bytes) {
$size = array('B','KB','MB','GB','TB');
$factor = floor(log($bytes, 1024));
$format = $factor > 2 ? '%.1F %s' : '%u %s';
return sprintf($format, $bytes / pow(1024, $factor), $size[$factor]);
}
@polonskiy
polonskiy / shingles.php
Created January 27, 2014 16:00
Near-duplicate finder. Shingling
<?php
function similar_text_shingles($texts, $shingle_len = 10, $algo = 'md5') {
$c = count($texts);
$total = 0;
for ($i = 0; $i < $c; $i++) {
preg_match_all('#\w{4,}#u', mb_strtolower($texts[$i], 'utf-8'), $matches);
$words = $matches[0];
$cc = count($words) - $shingle_len;
for ($j = 0; $j < $cc; $j++) {
@polonskiy
polonskiy / fsstorage.php
Created January 28, 2014 13:27
FileSystem Storage Concept
<?php
class FSStorage {
protected $dir;
public function __construct($dir) {
$this->dir = $dir;
}
@polonskiy
polonskiy / memcached2array.php
Last active January 28, 2019 11:16
dump memcached data
#!/usr/bin/php
<?php
$argv += [1 => 'localhost', 2 => 11211];
$memcached = new Memcached;
$memcached->addServer($argv[1], $argv[2]);
$result = [];
foreach($memcached->getAllKeys() as $k) $result[$k] = $memcached->get($k);
var_export($result);
echo ";\n";
@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 / PHProutines.php
Created December 4, 2014 09:32
PHProutines
<?php
class Runner {
public function __construct() {
pcntl_signal(SIGCHLD, SIG_IGN);
}
public function go() {
if (pcntl_fork()) return;