Skip to content

Instantly share code, notes, and snippets.

View Bujhm's full-sized avatar
🐢
I may be slow to respond.

Igor Bujhm

🐢
I may be slow to respond.
View GitHub Profile
@Bujhm
Bujhm / gzip.php
Created November 24, 2010 01:15
<?php // gzip.php v1.2 - read http://rm.pp.ru/?1.phpgzip // released on 2004-05-06, by Roman Mamedov<roman at rm.pp.ru> // license: do with this code whatever you want. ///// Configuration ////////////////// $PREFER_DEFLATE = false; // prefer
<?php
// gzip.php v1.2 - read http://rm.pp.ru/?1.phpgzip
// released on 2004-05-06, by Roman Mamedov<roman at rm.pp.ru>
// license: do with this code whatever you want.
///// Configuration //////////////////
$PREFER_DEFLATE = false; // prefer deflate over gzip when both are supported
$FORCE_COMPRESSION = false; // force compression even when client does not report support
//////////////////////////////////////
@Bujhm
Bujhm / pattern singleton in PDO.php
Created April 5, 2011 00:15
paul dot maddox at gmail dot com 27-Aug-2009 12:54
I decided to create a singleton wrapper for PDO that ensures only one instance is ever used.
It uses PHP 5.3.0's __callStatic functionality to pass on statically called methods to PDO.
This means you can just call it statically from anywhere without having to initiate or define the object.
Usage examples:
<?php
DB::exec("DELETE FROM Blah");
@Bujhm
Bujhm / Query Builder для Yii.php
Created April 5, 2011 07:41
Query Builder для Yii
public function buildQuery($query)
{
$sql=isset($query['distinct']) && $query['distinct'] ? 'SELECT DISTINCT' : 'SELECT';
$sql.=' '.(isset($query['select']) ? $query['select'] : '*');
if(isset($query['from']))
$sql.="\nFROM ".$query['from'];
else
throw new CDbException(Yii::t('yii','The DB query must contain the "from" portion.'));
@Bujhm
Bujhm / function timespan.php
Created November 1, 2011 11:04
Функция таймлайн о времени написания твиттов: 1 час назад, около 7 дней назад, менее минуты назад и т.д
function timespan($time1, $time2 = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds')
{
$output = preg_split('/[^a-z]+/', strtolower((string) $output));
if (empty($output))
return FALSE;
extract(array_flip($output), EXTR_SKIP);
$time1 = max(0, (int) $time1);
$time2 = empty($time2) ? time() : max(0, (int) $time2);
$timespan = abs($time1 - $time2);
isset($years) and $timespan -= 31556926 * ($years = (int) floor($timespan / 31556926));
@Bujhm
Bujhm / GetLastHeaderStatus.php
Last active April 24, 2017 08:19
GetLastHeaderStatus got from php.net and simply wrapped in a function
<?php
/**
* Get Last Header (no need to follow by redirect manually)
* @param $url
* @return array
*/
function getLastHeaderStatus($url)
{
$result = array();
$header = get_headers( $url, 1 );
@Bujhm
Bujhm / colorize.php
Created April 26, 2017 22:31
Generating Command Line Colors with PHP
<?php
/**
* Generating Command Line Colors with PHP
* @author Mario Awad
* @link http://softkube.com/blog/generating-command-line-colors-with-php
*/
function colorize($text, $status) {
$out = "";
switch($status) {
case "SUCCESS":
@Bujhm
Bujhm / memory_consumption.php
Created July 22, 2017 21:56
[CheckMemoryConsumption проверка занимаемой памяти (basic implementation))
/**
* [CheckMemoryConsumption проверка занимаемой памяти приложением]
* @param string $state [состояние одно из возможных: initial,final,peak]
* @return string
*/
public static function CheckMemoryConsumption($state = 'initial')
{
$memorySizeConsumptionString = '';
switch ($state){
@Bujhm
Bujhm / Unicode (\xyz) decoding.php
Last active July 23, 2017 15:25
Декодим unicode символы просто
<?php
// декодим Unicode ( \xc3\xa9 )
$a = array("\xc3\xa9");
echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n";
@Bujhm
Bujhm / jquery.email-antispam.js
Created February 15, 2018 15:00 — forked from mathiasbynens/jquery.email-antispam.js
Simple spam protection for email addresses using jQuery
/* Simple spam protection for email addresses using jQuery.
* Well, the protection isn’t jQuery-based, but you get the idea.
* This snippet allows you to slightly ‘obfuscate’ email addresses to make it harder for spambots to harvest them, while still offering a readable address to your visitors.
* E.g.
* <a href="mailto:foo(at)example(dot)com">foo at example dot com</a>
* →
* <a href="mailto:[email protected]">[email protected]</a>
*/
$(function() {
@Bujhm
Bujhm / detect_ajax.php
Created March 28, 2018 23:59
detect ajax
<?php // got from here https://www.sanwebe.com/2011/02/how-to-detect-ajax-request-using-php
//detect ajax
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die('Request must come from Ajax!');
}