Skip to content

Instantly share code, notes, and snippets.

View aramk's full-sized avatar

Aram Kocharyan aramk

View GitHub Profile
@aramk
aramk / url_std.php
Created February 23, 2011 02:12
Simple URL Standardization that takes a user input URL and attempts to standardize it. If no URL scheme is given, defaults to http, also appends a forward slash if needed.
<?php
function url_std($url) {
extract(parse_url($url));
if ($scheme == '') {
$scheme = 'http';
}
$url = $scheme . "://" . $host . $path;
// Append a forward slash if needed
if ( !preg_match('|/$|', $url) ) {
@aramk
aramk / email_scrape.php
Created June 30, 2011 12:33
Scrape emails from a given URL in PHP. Using it to invite people to Google+, for now :)
<?php
$url = 'http://computerandu.wordpress.com/2011/06/29/how-to-get-google-invite/';
$emails = scrape_email($url);
echo implode($emails, ' ');
function scrape_email($url) {
if ( !is_string($url) ) {
return '';
}
@aramk
aramk / sample.php
Created August 8, 2011 07:52
Sample PHP Class
<?php
// A sample class
class Human {
$age = 0;
function birthday() {
$age++;
echo 'Happy Birthday!';
}
}
?>
@aramk
aramk / ordict.py
Created October 17, 2011 13:00
Python Ordered Dictinary
# -*- coding: utf-8 -*-
'''
Ordered dict
http://ak.net84.net/projects/python-ordered-dictionary/
Example usage:
>>> o = Ordict( ('abc', 123), ('def', '456', 3) )
>>> o['ghi'] = '789'
@aramk
aramk / list.py
Created October 18, 2011 13:53
Expand recursive list in Python
def expand_list(list_of_lists):
list = []
for i in list_of_lists:
if type(i) is type([]):
i = expand_list(i)
for j in i:
list.append(j)
else:
list.append(i)
return list
@aramk
aramk / paragraphs.php
Created March 19, 2012 12:40
// Remove the last unfinished sentence in a paragraph if it contains $min_word_count or less words
// Remove the last unfinished sentence in a paragraph if it contains $min_word_count or less words
function remove_unfinished_sentence($paragraph, $min_word_count = 3, $replace = '.') {
$min_word_count = $min_word_count > 1 ? $min_word_count : 1;
return preg_replace('#(\.\s*([^\s\.]+\s+){0,'.($min_word_count-1).'}([^\s\.]+\s*))$#ms', $replace, $paragraph);
}
@aramk
aramk / gist:3011743
Created June 28, 2012 14:43
Add placeholder support to all browsers
// Derived from http://www.cssnewbie.com/example/placeholder-support/
// I needed to capture both "text" and "email" input types.
// This adds 'placeholder' to the items listed in the jQuery .support object.
jQuery(function() {
jQuery.support.placeholder = false;
test = document.createElement('input');
if('placeholder' in test) jQuery.support.placeholder = true;
});
@aramk
aramk / gist:3191662
Created July 28, 2012 03:35
JavaScript Module Pattern
// My take on the JavaScript module pattern
// By Aram Kocharyan, 2012.
// http://ak.net84.net/
(function($) {
window.moduleName = new function() {
var base = this;
// Internally visible
@aramk
aramk / placeholder.js
Created August 14, 2012 11:44
Adds placeholder to browsers that don't support it (IE)
// Adds placeholder to browsers that don't support it (IE)
$(document).ready(function() {
if(!$.support.placeholder) {
var sel = "*[placeholder]";
var addPlaceholders = function () {
if ($(this).attr('placeholder') && $(this).val() == "") {
$(this).val($(this).attr('placeholder'));
$(this).addClass('placeholder');
}
@aramk
aramk / gist:3464169
Created August 25, 2012 11:31
Current URL in JavaScript
var loc = window.location;
var currentURL = loc.protocol + '//' + loc.host + loc.pathname;