Skip to content

Instantly share code, notes, and snippets.

View sobstel's full-sized avatar

Przemek Sobstel sobstel

View GitHub Profile
@sobstel
sobstel / clipped_text.css
Created March 18, 2011 17:18
Clipped text in CSS
.clipped_text {
white-space: nowrap;
width: 100%; # for IE 6
overflow: hidden;
text-overflow: ellipsis;
}
@sobstel
sobstel / gist:919114
Created April 14, 2011 08:15
Mac OS X: redirecting port 80 to actual http port
We couldn’t find that file to show.
@sobstel
sobstel / mysql_fast_load.sql
Created May 8, 2011 14:59
MySQL fast data load
#1
LOAD DATA INFILE...
#2
ALTER TABLE name DISABLE KEYS; // tells MySQL to stop updating nonunique indexes
INSERT ...
...
ALTER TABLE name ENABLE KEYS; // re-creates missing indexes (much faster than inserting one by one)
#3 (InnoDB)
@sobstel
sobstel / ternary_operator_gotcha.php
Created May 12, 2011 18:45
PHP new ternary operator gotcha
<?php
// old way
$value = $value ? $value : false;
// new way
$value = $value ?: false;
// however...
$value = isset($value) ?: false; // if eg. $value="value", then it returns (bool)true, and not (string)"value"
@sobstel
sobstel / accessing_private.php
Created May 17, 2011 09:11
Accessing private members of the same object type
<?php
class Test
{
private $foo;
public function __construct($foo)
{
$this->foo = $foo;
}
@sobstel
sobstel / pdo_group_results.php
Created May 31, 2011 19:03
PHP PDO grouping results
<?php
$stmt = $pdo_connection->query('SELECT id, name, surname FROM people');
$people = $stmt->fetchAll(\PDO::FETCH_GROUP | \PDO::FETCH_UNIQUE | \PDO::FETCH_ASSOC);
/*
$people will now contain:
array(
1 => array(
'name' => 'Juan',
'surname' => 'Sorin',
@sobstel
sobstel / analyzer.rb
Created June 12, 2011 12:27
Apache log analyzer
#!/usr/bin/env ruby
# Apache log analyzer
# by Przemek Sobstel (przemek@sobstel.org)
require 'rubygems'
require 'slop'
require 'time'
class Array
@sobstel
sobstel / regex_look_ahead_and_behind.rb
Created June 23, 2011 14:51
Regex look ahead and look behind
# (?=) Positive look ahead assertion foo(?=bar) matches foo when followed by bar.
"Best player ever is Leo Messi.".match(/Leo(?=\s?Messi)\s\w+/)
=> ["Leo Messi"]
# (?!) Negative look ahead assertion foo(?!bar) matches foo when not followed by bar.
> "Or maybe Javier Mascherano? Or Javier Zanetti?.".scan(/Javier(?!\s?Mascherano)\s\w+/)
=> ["Javier Zanetti"]
# (?<=) Positive look behind assertion (?<=foo)bar matches bar when preceded by foo.
"Some say it is Sergio Aguero, but Sergio Batista does not let him play much".scan(/\w+\s(?<=Sergio\s)\w+/)
@sobstel
sobstel / datetime_timezones.php
Created September 9, 2011 16:52
PHP: Converting DateTime between timezones
<?php
// now
$date = date_create('2010-12-31 22:00:01', timezone_open('Europe/Amsterdam'));
// Buenos Aires
date_timezone_set($date, timezone_open('America/Argentina/Buenos_Aires'));
echo $date->format('Y-m-d H:i:s');
// 2010-12-31 18:00:01
// UTC
@sobstel
sobstel / devise.find_by_username_or_email.rb
Created September 28, 2011 21:30
Devise: find by username OR email
class User
extend ClassMethods
devise :database_authenticatable, :omniauthable, :registerable, :confirmable, :recoverable, :rememberable
module ClassMethods
def find_for_database_authentication(conditions)
value = conditions[authentication_keys.first]
where(["username = :value OR email = :value", { :value => value }]).first
end