Skip to content

Instantly share code, notes, and snippets.

View kobus1998's full-sized avatar
😳
-10x programmer

Kobus kobus1998

😳
-10x programmer
View GitHub Profile
<?php
private function pdo (): \PDO
{
return new \PDO("
mysql:host={$this->config['host']};
port={$this->config['port']};
dbname={$this->config['dbname']}",
$this->config['user'],
$this->config['pass']);
@kobus1998
kobus1998 / Pipeline.php
Created January 22, 2018 15:59
Short pipeline function
<?php
function pipeline ($val, array $pipes)
{
foreach($pipes as $pipe)
{
$val = $pipe($val);
}
return $val;
@kobus1998
kobus1998 / index.php
Created February 1, 2018 10:27
Replace multiple whitespace to single whitespace
<?php
$str = "a bc
d e f";
$regex = "/[^\S\n]+/";
$str = preg_replace($regex, " ", $str);
echo $str;
@kobus1998
kobus1998 / regex.php
Created February 2, 2018 09:26
regex url
<?php
$URI = "https://msdn.microsoft.com/en-us/library/system.net.httplistener(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1";
$regex = "(http[s]?:\/\/)?([^\/\s]+\/)(.*)(\?.*[\&]?)";
preg_match("/$regex/", $URI, $match);
print_r($match);
/*
Array
@kobus1998
kobus1998 / index.php
Last active February 4, 2018 18:58
function capture
<?php
$regex = "(?<function>(?:int)\s*\w+\s*\((?:(?:int)\s*\w+\s*\,\s*){0,}+(?:int)\s*\w+\s*\)\s*\{\s*.*\s*\})";
$str = "
int count(int i, int a)
{
return i++;
}
";
@kobus1998
kobus1998 / if.php
Last active February 4, 2018 19:12
Capture if else if else
<?php
$pattern = "(?<if_all>(?<if>(?:if)\s*\(\s*.*\s*\)\s*\{\s*.*\s*\}\s*)\s*(?<elseif>(?:(?:else if|elseif)\s*\(\s*.*\s*\)\s*\{\s*.*\s*\}\s*){0,}+)\s*(?<else>(?:else)\s*\{\s*.*\s*\}\s*){0,1})";
$haystack = "
if(x)
{
xxxxx
}
else if (x)
@kobus1998
kobus1998 / arrayToXML.php
Created February 27, 2018 11:00
array to xml
<?php
$arr = [
'Rooms' => [
'1' => [
'name' => 'xxx',
'desc' => 'xxx'
],
'2' => [
'name' => 'yyy',
<?php
class Benchmark
{
public $time;
private $startTime;
private $stopTime;
public function go($callback)
{
@kobus1998
kobus1998 / index.php
Created March 8, 2018 12:53
array to xml
<?php
$xml = [
"payments" => [
[
"amount" => "100",
"initials" => "ih"
],
[
"amount" => "1000",
@kobus1998
kobus1998 / index
Created March 8, 2018 14:27
url match
(?:(?<http>http(?:s*)):\/\/(?<host>staging.roomraccoon.com))*(?<path>(?:\/\w*)*)(?<qs>(?:\?\w*=\w*)(?:\&\w*=\w*))