Skip to content

Instantly share code, notes, and snippets.

@djheru
djheru / xslt_transform
Created February 11, 2013 12:36
XSLT Transformations with PHP
function xslt_transform($xslFile, $xmlStr)
{
$xsl = new DomDocument();
$xsl->load($xslFile, LIBXML_NOCDATA);
$xml = new DomDocument();
$xml->loadXML($xmlStr);
$xslt = new XSLTProcessor();
$xslt->registerPHPFunctions();
@djheru
djheru / gist:5452641
Last active December 16, 2015 14:59
Singleton in Python
class Singleton(object):
class __metaclass__(type):
def __call__(cls, *args, **kwargs):
if not getattr(cls, 'instance', None):
print 'Creating instance'
cls.instance = cls.__new__(cls, *args, **kwargs)
print 'Created %s' % id(cls.instance)
cls.instance.__init__(*args, **kwargs)
return cls.instance
@djheru
djheru / gist:5548298
Last active December 17, 2015 04:08
Fartscroll!
<script src="http://code.onion.com/fartscroll.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
fartscroll(200);
});
</script>
@djheru
djheru / gist:6699402
Created September 25, 2013 13:13
Apache configuration for mod_wsgi
<VirtualHost *>
ServerName hostname.com
ServerAlias www.hostname.com
DocumentRoot /var/www/hostname.com/htdocs/
DirectoryIndex index.php index.html index.htm
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
@djheru
djheru / php_file_encrypt.php
Created February 25, 2014 13:49
Ecrypt file with PHP
<?php
function encryptData($value){
$key = "top secret key";
$text = $value;
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
return $crypttext;
}
@djheru
djheru / array_csv_download.php
Last active August 29, 2015 13:56
Array to CSV
<?php
function convert_to_csv($input_array, $output_file_name, $delimiter)
{
/** open raw memory as file, no need for temp files */
$temp_memory = fopen('php://memory', 'w');
/** loop through array */
foreach ($input_array as $line) {
/** default php csv handler **/
fputcsv($temp_memory, $line, $delimiter);
@djheru
djheru / php_pluck.php
Last active August 29, 2015 13:56
array_filter() nested arrays using an external value
s<?php
function pluck($nestedArray, $key, $value, $maintainKeys=true){
$func = 'return ($obj["' . $key . '"] == "' . $value . '") ? true : false;';
$callback = create_function('$obj', $func);
$filtered = array_filter($nestedArray, $callback);
return ($maintainKeys) ? $filtered : array_values($filtered);
}
$users = [
@djheru
djheru / js-ordinal-number-suffix.js
Created February 28, 2014 18:47
Formatter for numbers like 1st, 2nd, 3rd, 4th, etc (Ordinal Number Suffix)
var ons = function (n) {return n < 11 || n > 13 ? ['st', 'nd', 'rd', 'th'][Math.min((n - 1) % 10, 3)] : 'th'};
for(var i=1; i <= 100; i++){ console.log(i + '' + ons(i)); }
@djheru
djheru / ZF2EventExample.php
Last active August 29, 2015 13:58
ZF2 event example
<?php
if (file_exists('vendor/autoload.php')) {
$loader = include 'vendor/autoload.php';
} else {
throw new Exception('Composer dependencies have not been installed');
}
use Zend\EventManager\EventCollection,
Zend\EventManager\EventManager;
@djheru
djheru / bash_aliases.sh
Last active August 29, 2015 14:00
My bash aliases
alias http='python -m SimpleHTTPServer'
function featurebranch() { git checkout master && git pull upstream master && git checkout -b "$1"; }
function newbox() { git clone git@github.com:djheru/vagrant-template.git "$1"; }
function ignore() { git update-index --assume-unchanged "$1"; }
function unignore() { git update-index --no-assume-unchanged "$1"; }
alias l='ls -lah'
alias ..='cd ..'
alias snif='phpcs -v -n -p --standard=PSR2 ./'