Skip to content

Instantly share code, notes, and snippets.

View egulhan's full-sized avatar
Here we go again...

Erman Gülhan egulhan

Here we go again...
View GitHub Profile
@egulhan
egulhan / sed-from-grep.sh
Created January 31, 2014 15:49
how to execute sed command from just files result from grep command
grep -rl "'filter'=>FrontEnd::createPhInputField" * | grep -v ".svn" | xargs -l sed -i "s/filter'=>FrontEnd::createPhInputField(\(.*\),\(.*\))/filter'=>FrontEnd::createPhInputField(\1,\2,\$filtersForm)/g"
@egulhan
egulhan / detectAndRemoveFileWithBoom
Created March 4, 2014 14:08
How-to detect file with BOM and remove it
# Source: http://blog.x-if.com/2010/12/find-utf-8-files-with-bom/
# http://stackoverflow.com/questions/204765/elegant-way-to-search-for-utf-8-files-with-bom
find -type f|while read file;do [ "`head -c3 -- "$file"`" == $'xefxbbxbf' ] && echo "found BOM in: $file";done
grep -orHbm1 "^`echo -ne 'xefxbbxbf'`" . | sed '/:0:/!d;s/:0:.*//'
# most efficient way:
find . -type f -print0 | xargs -0r awk '/^xEFxBBxBF/ {print FILENAME} {nextfile}'
@egulhan
egulhan / recursiveLdapDelete.php
Created March 7, 2014 14:38
Recursive version of ldap_delete
// source: http://www.php.net/manual/en/function.ldap-delete.php
function myldap_delete($ds,$dn,$recursive=false){
if($recursive == false){
return(ldap_delete($ds,$dn));
}else{
//searching for sub entries
$sr=ldap_list($ds,$dn,"ObjectClass=*",array(""));
$info = ldap_get_entries($ds, $sr);
for($i=0;$i<$info['count'];$i++){
@egulhan
egulhan / Lock.php
Created March 19, 2014 20:02
Script locking mechanism for one-time-running scripts such as cron scripts
<?php
/**
* Class Lock
* Script locking mechanism for one-time-running scripts.
*/
class Lock
{
private $fp;
function __construct()
@egulhan
egulhan / set_ES_HEAP_SIZE
Created March 31, 2014 10:56
How-to set ES_HEAP_SIZE variable for Elasticsearch installed as package
# ES version: 0.90.5
# /etc/init.d/elasticsearch
ES_HEAP_SIZE=4g
@egulhan
egulhan / footerInGridView.php
Created April 10, 2014 07:39
Footer column usage in GridView, Yii
<?php
/**
* Can be used as sum of a column
*/
...
array(
'name'=>'first_col',
'footer'=>'<b>TOTAL:</b>'
),
@egulhan
egulhan / convertBytes.php
Created April 17, 2014 11:47
Convert bytes to up-sizes
/**
* @source: http://codeaid.net/php/convert-size-in-bytes-to-a-human-readable-format-(php)
*/
function bytesToSize1024($bytes, $precision = 2)
{
$unit = array('B','KB','MB','GB','TB','PB','EB');
return @round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), $precision).' '.$unit[$i];
}
@egulhan
egulhan / multipleInheritance.php
Created May 16, 2014 13:15
PHP does not support multiple inheritance. This shows how to make it possible.
// Source: http://stackoverflow.com/a/356431
class B {
public function method_from_b($s) {
echo $s;
}
}
class C {
public function method_from_c($s) {
@egulhan
egulhan / downloadFile.php
Created June 30, 2014 11:54
Make browser download a file without saving the it.
@source: http://php.net/manual/en/function.readfile.php
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
@egulhan
egulhan / base64UsingJquery.js
Created July 1, 2014 07:24
Encode/decode with/from base64 using jQuery
@source: http://www.naveen.com.au/javascript/jquery/encode-or-decode-html-entities-with-jquery/289
function htmlEncode(value){
if (value) {
return jQuery('<div />').text(value).html();
} else {
return '';
}
}