Skip to content

Instantly share code, notes, and snippets.

View glagola's full-sized avatar

Igor Glagola glagola

View GitHub Profile
@glagola
glagola / gist:1884392
Created February 22, 2012 11:38
Generate unique name of the new file by full path to the file
<?php
function genUniqueFileName($fullPathToFile)
{
$pathInfo = pathinfo($fullPathToFile);
$k = 1;
$uniquePath = $fullPathToFile;
while (file_exists($uniquePath)) {
$uniquePath = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '-' . $k++ . '.' . $pathInfo['extension'];
}
@glagola
glagola / gist:2299962
Created April 4, 2012 09:32
utf8_strrev($str)
<?php
function utf8_strrev($str) {
return iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
}
<?php
function mysql_escape_mimic($inp)
{
if(!empty($inp) && is_string($inp)) {
return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $inp);
}
return $inp;
}
@glagola
glagola / google-chrome-optimizer
Created May 31, 2012 11:01
google-chrome-optimizer
#! /bin/sh
### BEGIN INIT INFO
# Provides: skeleton
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Example initscript
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d.
@glagola
glagola / gist:2843814
Created May 31, 2012 14:36
Get first day of previous month
<?php
$t = strtotime('first day of previous month');
@glagola
glagola / gist:3447864
Created August 24, 2012 09:07
List Files deeply
<?php
function files($dir, $callback) {
$queue = array($dir);
$r = -1;
$w = 0;
while ($r < $w) {
$path = $queue[++$r];
@glagola
glagola / smartCut
Created September 25, 2012 07:51
Обрезает строку по длине, не разрезая слова. Корректно работает с UTF-8.
<?php
function smartCut($str, $len, $enc = 'UTF-8') {
return (mb_strlen($str, $enc) > $len) ? mb_substr($str, 0, mb_strpos($str, ' ', $len, $enc), $enc) : $str;
}
@glagola
glagola / gist:3931984
Created October 22, 2012 15:17
file_get_contents proxy
<?php
$url = 'http://www';
$proxy = 'tcp://xxx:8080';
$context = array(
'http' => array(
'proxy' => $proxy,
'request_fulluri' => True,
@glagola
glagola / gist:3942156
Created October 23, 2012 22:32
Распознавание кодировки строки
<?php
function detect_encoding($string, $pattern_size = 50)
{
$list = array('cp1251', 'utf-8', 'ascii', '855', 'KOI8R', 'ISO-IR-111', 'CP866', 'KOI8U');
$c = strlen($string);
if ($c > $pattern_size)
{
$string = substr($string, floor(($c - $pattern_size) /2), $pattern_size);
$c = $pattern_size;
@glagola
glagola / gist:4109816
Created November 19, 2012 09:26
Транслитирирует русский текст в английский
<?php
function rus2translit($text)
{
// Русский алфавит
$rus_alphabet = array(
'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й',
'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф',
'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я',
'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й',