Skip to content

Instantly share code, notes, and snippets.

View macedocodigo's full-sized avatar

David Macedo macedocodigo

View GitHub Profile
@macedocodigo
macedocodigo / folder check
Created November 29, 2010 20:53
makes sure the full path we want to work with exists
function checkFolders($path) {
$bits = explode(DIRECTORY_SEPARATOR, $path);
$sandbox = '';
foreach($bits as $bit) {
$sandbox .= $bit . DIRECTORY_SEPARATOR;
if(!is_dir($sandbox)) {
mkdir($sandbox);
}
}
@macedocodigo
macedocodigo / flatten files in folder to array
Created December 1, 2010 17:54
recursive function to find all files in subdirectories and push them into a flat array
$files = array();
function returnFiles($dir) {
global $files;
$dh = opendir($dir);
while(false !== ($filename = readdir($dh))) {
if($filename == '.' || $filename == '..') {
continue;
}
echo $dir.DIRECTORY_SEPARATOR.$filename . "\n";
if(is_file($dir.DIRECTORY_SEPARATOR.$filename)) {
@macedocodigo
macedocodigo / string.prototype.clip.js
Created January 21, 2011 17:18
Reduce a string's length by clipping characters off the end, adding optional characters to the end such as ellipsis
/*
** String prototype for clipping text and optionally adding ellipsis
** Does not modify original variable
** -Example-
** var thisStr = 'Some string of text';
** var newStr = thisStr.clip(10, '...', true); // Some...
** var newStr = thisStr.clip(15, '...', true); // Some string of...
*/
String.prototype.clip = function(len/* Number */, str/* String */, bound/* Boolean */) {
var origLen = len;
@macedocodigo
macedocodigo / foldersToArray.php
Created February 1, 2011 19:16
Recursively scan files and folders in a directory, building into either a multidimensional array or flattened array of full paths
function foldersToArray($path, $flat = false) {
$folderArray = array();
if($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if($file != '.' && $file != '..') {
if(!in_array($path.'/'.$file, $folderArray) && is_file($path.'/'.$file)) {
if($flat) {
array_push($folderArray, $path.'/'.$file);
} else {
array_push($folderArray, $file);
@macedocodigo
macedocodigo / in_array_exclusively.php
Created March 2, 2011 22:33
Check whether a value or array of values are the only existing values within an array
function in_array_exclusive($needles, $haystack) {
$keys = count($haystack);
if(is_array($needles)) {
foreach($needles as $needle) {
if(!in_array($needle, $haystack)) {
return false;
} else {
$keys = $keys - 1;
}
}
@macedocodigo
macedocodigo / removeEmptySubfolders.php
Created March 9, 2011 17:42
Scan through a given directory, removing empty directories
function removeEmptySubFolders($path) {
$empty = true;
foreach (glob($path.DIRECTORY_SEPARATOR."*") as $file) {
$empty &= is_dir($file) && RemoveEmptySubFolders($file);
}
return $empty && rmdir($path);
}
@macedocodigo
macedocodigo / unshift.prototype.js
Created November 8, 2011 20:02
js prototype of unshift function
if(!Array.prototype.unshift) { // if this method does not exist..
Array.prototype.unshift = function(){
this.reverse();
for(var i=arguments.length-1;i>=0;i--){
this[this.length]=arguments[i]
}
this.reverse();
@macedocodigo
macedocodigo / shift.prototype.js
Created November 8, 2011 20:02
js prototype of shift function
if(!Array.prototype.shift) { // if this method does not exist..
Array.prototype.shift = function(){
firstElement = this[0];
this.reverse();
this.length = Math.max(this.length-1,0);
this.reverse();
return firstElement;
}
@macedocodigo
macedocodigo / intersect.js
Created October 15, 2012 16:34
JS: Multi-array intersect
/*
** intersect.js
** intersect multiple arrays
** Usage:
var arr1 = ['hey', 'you', 'guys', 'test', 'there'];
var arr2 = ['there', 'you'];
var arr3 = ['some', 'there', 'you', 'thrice', 'hey', 'huzzah'];
intersect(arr1, arr2, arr3);
*/
function intersect() {
@macedocodigo
macedocodigo / getAbsoluteUrl.js
Created July 23, 2013 17:07
Provided a url and a path relative to that url, this function will return the resulting absolute url
var getAbsoluteUrl = function(url, path) {
if(url.substr(-1) == '/') {
url = url.substr(0, url.length-1);
}
var path = path;
if(path !== null) {
if(url.substr(-1) == "/") {
url = url.substr(0, url.length-1);