Skip to content

Instantly share code, notes, and snippets.

View nicklasos's full-sized avatar
💭
😅

Olkhovyk Mykyta nicklasos

💭
😅
View GitHub Profile
<?php
/**
* Depend from csv_to_array and get_in functions
* @link https://gist.github.com/nicklasos/11251754
* @link https://gist.github.com/nicklasos/9206376
*/
function t($phrase)
{
static $dictionary;
<?php
if (!function_exists('array_column')) {
function array_column(array $input, $columnKey, $indexKey = null) {
$result = array();
if (null === $indexKey) {
if (null === $columnKey) {
// trigger_error('What are you doing? Use array_values() instead!', E_USER_NOTICE);
$result = array_values($input);
@nicklasos
nicklasos / json.cs
Last active August 29, 2015 14:01
C# Common functions
public struct MainPage
{
public string Threads { get; set; }
public List<Pager> Pages { get; set; }
}
public struct Pager
{
private string filename;
public string Filename
@nicklasos
nicklasos / mongoload.php
Last active August 29, 2015 14:02
Insert random data to mongo
<?php
include __DIR__ . '/vendor/autoload.php';
$faker = Faker\Factory::create();
$randData = [
'gmt_diff' => 'randomDigitNotNull',
'ios_id' => 'uuid',
'email' => 'email',
@nicklasos
nicklasos / varname.php
Last active August 29, 2015 14:02
Variable name
<?php
function variable_name(&$var)
{
$old = $var;
$var = $new = 'UNIQUE' . mt_rand() . 'VARIABLE';
$vname = false;
foreach($GLOBALS as $key => $val) {
if($val === $new) {
$vname = $key;
@nicklasos
nicklasos / mongo_id_from_time.php
Created July 2, 2014 12:56
Mongo id from time
<?php
function mongo_id_from_time($yourTimestamp)
{
static $inc = 0;
$ts = pack('N', $yourTimestamp);
$m = substr(md5(gethostname()), 0, 3);
$pid = pack('n', posix_getpid());
$trail = substr(pack('N', $inc++), 1, 3);
@nicklasos
nicklasos / download.php
Last active November 29, 2024 12:56
Curl PHP multiple files downloading
<?php
function multiple_download(array $urls, $save_path = '/tmp')
{
$multi_handle = curl_multi_init();
$file_pointers = [];
$curl_handles = [];
// Add curl multi handles, one per file we don't already have
foreach ($urls as $key => $url) {
@nicklasos
nicklasos / prompt_password.php
Last active August 29, 2015 14:05
Prompt password
<?php
/**
* Interactively prompts for input without echoing to the terminal.
* Requires a bash shell or Windows and won't work with
* safe_mode settings (Uses `shell_exec`)
*
* @see http://us.php.net/manual/en/function.ncurses-noecho.php
* @see http://us.php.net/manual/en/function.ncurses-getch.php
*
* Works only on normal OS, not in windows.
@nicklasos
nicklasos / plists.erl
Last active November 14, 2017 10:40
Erlang. Parallel map
-module(plists).
-export([pmap/2,pforeach/2,npforeach/2, parmap/2]).
%%% Map
pmap(F, L) ->
S = self(),
Pids = lists:map(fun(I) -> spawn(fun() -> pmap_f(S, F, I) end) end, L),
pmap_gather(Pids).
@nicklasos
nicklasos / retry.php
Created September 23, 2014 08:52
retry
<?php
function retry($retries, callable $fn)
{
beginning:
try {
return $fn();
} catch (\Exception $e) {
if (!$retries) {
throw new \Exception();