The online racing simulator
PHP Function Dump
(20 posts, started )
PHP Function Dump
List all files in a directory, and in sub directory's, return as array.

<?php 
php
function get_dir_structure($path$recursive true) {
    if (
is_dir($path)) {
        if (
$handle opendir($path)) {
            while (
false !== ($item readdir($handle))) {
                if (
$item != '.' && $item != '..') {
                    if (
is_dir($path $item)) {
                        if (
$recursive) {
                            
$return[$item] = get_dir_structure($path $item '/');
                        } else {
                            
$return[$item] = array();
                        }
                    } else {
                        
$return[] = $item;
                    }
                }
            }
            
closedir($handle);
        }
        return 
$return;
    } else {
        
trigger_error('$path is not a directory!'E_USER_WARNING);
        return 
FALSE;
    }
}

print_r(get_dir_structure('/home/dygear/My Music/'));
?>

This is one of the most useful function I have made. Please, add your own!
Hiss, see I know python. .
#3 - filur
Quote from MoHaX :python

Fails on forced indentation of code.

Dygear, check out RecursiveIterator and RecursiveDirectoryIterator in SPL.
just like how i once spent half a day writing a binary float value to php variable conversion, only to find out about the unpack function later on
Quote from Victor :just like how i once spent half a day writing a binary float value to php variable conversion, only to find out about the unpack function later on

Yeah, unpack rox!

Quote from filur :Fails on forced indentation of code.

Dygear, check out RecursiveIterator and RecursiveDirectoryIterator in SPL.

I did not need anything that insane.
#6 - filur
Quote from Dygear :I did not need anything that insane.


<?php 
function recursive($path) {
    
$iit = new RecursiveIteratorIterator($it = new RecursiveDirectoryIterator($path));
    foreach (
$iit as $file) {
        echo 
$file->getPath() . $file->getFilename() . PHP_EOL;
    }
}
?>

Every coder should write at least a couple of recursive functions in their life.
Quote from Anarchi-H :Every coder should write at least a couple of recursive functions in their life.


<?php 
function int2repString($int) {
    
$unit = array(1=>'One',2=>'Two',3=>'Three',4=>'Four',5=>'Five',6=>'Six',7=>'Seven',8=>'Eight',9=>'Nine');
    
$tens = array(2=>'Twenty',3=>'Thirty',4=>'Forty',5=>'Fifty',6=>'Sixty',7=>'Seventy',8=>'Eighty',9=>'Ninety');
    if (
$int == 0)
        return 
'Zero';
    if (
$int && $int 10)
        return 
$unit[$int];
    if (
$int == 10)
        return 
'Ten';
    if (
$int 10 && $int 20) {
        switch(
$int) {
            case 
11: return 'Eleven';
            case 
12: return 'Twelve';
            case 
13: return 'Thirteen';
            case 
14: return 'Fourteen';
            case 
15: return 'Fifthteen';
            default: return 
$unit[substr($int11)] . 'teen';
        }
    }
    if (
$int 10 == 0)
        return 
$tens[$int 10];
    if (
$int 20 && $int 100)
        return 
$tens[substr($int01)] . $unit[substr($int11)];
    return 
FALSE;
}
?>

Simple XSV Parser (CSV, TSV ...)

<?php 
php
function xsv($file$delimiter ',') {
    
$return = array();
    
$row 1;
    if ((
$xsv fopen($file'r')) !== FALSE) {
        while ((
$line fgetcsv($xsv0$delimiter)) !== FALSE) {
            
$j count($line);
            if (!isset(
$keys)) {
                
$keys = array();
                for (
$i 0$i $j$i++) {
                    
$keys[$i] = trim($line[$i]);
                }
            } else {
                for (
$i 0$i $j$i++) {
                    
$return[$row][$keys[$i]] = trim($line[$i]);
                }
                
$row++;
            }
        }
        return 
$return;
    } else {
        return 
false;
    }    
}
?>

#11 - wien

<?php 
$eighteen 
int2repString(18);
$fifteen int2repString(15);
if(
strcmp($eighteen'Eighteen') != || strcmp($fifteen 'Fifteen') != 0)
{
    echo 
'Spelling error';
}
?>

C#-style delegates, just for fun.
Attached files
delegates.zip - 2.2 KB - 177 views
PHP Function : Extract (The Function is Not Given Enough Credit)

<?php 
function get_dir_structure($path$recursive true$ext null) {
    
$return NULL;
    if (!
is_dir($path)) {
        
trigger_error('$path is not a directory!'E_USER_WARNING);
        return 
FALSE;
    }
    if (
$handle opendir($path)) {
        while (
FALSE !== ($item readdir($handle))) {
            if (
$item != '.' && $item != '..') {
                if (
is_dir($path $item)) {
                    if (
$recursive) {
                        
$return[$item] = get_dir_structure($path $item '/'$recursive$ext);
                    } else {
                        
$return[$item] = array();
                    }
                } else {
                    if (
$ext != null && strrpos($item$ext) !== FALSE) {
                        
$return[] = $item;
                    }
                }
            }
        }
        
closedir($handle);
    }
    return 
$return;
}

$path 'C:/Documents and Settings/Dygear/My Documents/My Music/';
print_r(get_dir_structure($pathtrue'.mp3'));

?>

Yes, my user name on my computer really is Dygear. It's for the times just like this where I have to give a code example and I don't really have to change anything should I fell the need to hide my real name.
PHP Function : Get Dir Structure get_dir_structure
With Comments.

<?php 
function get_dir_structure($path$recursive TRUE$ext FALSE) {
    
// This function Requires PHP5 >= 5.2.0;
    # Initialize Return Variable..
    
$return = array();
    
# Check for a '/' at the end of the path, append if not there.
    
if (substr($path, -1) != '/') {
        
$path .= '/';
    }
    
# Check to see if the $path is a directory, fail if it's not.
    
if (!is_dir($path)) {
        
trigger_error('$path ' $path ' is not a directory!'E_USER_WARNING);
        return -
1;
    }
    
# Check to see if we can open the directory, fail if we can't.
    
if (($handle = @opendir($path)) === FALSE) {
        
print_r(error_get_last());
        
trigger_error('Unable to open directory!'E_USER_WARNING);
        return -
2;
    }
    
# A, possibly, recursive read of the directory sturcture.
    
while (FALSE !== ($item readdir($handle))) {
        
# Check to see if the $item is the up or current handle, skip it if yes.
        
if ($item == '.' || $item == '..')
            continue;
        
# Check to see if the item is it's self a directory.
        
if (is_dir($path $item)) {
            
# Check to see if we are going to read the contents of this directory.
            
if ($recursive)
                
$return[$item] = get_dir_structure($path $item '/'TRUE$ext);
            else
                
$return[$item] = array();
        } else {
            
# Check to see if we are filtering for one extension.
            
if ($ext != FALSE && strrpos($item$ext) !== FALSE)
                
$return[] = $item;
        }
    }
    
# Close the directory handle.
    
closedir($handle);
    
# Return the results.
    
return $return;
}
?>

Without Comments.

<?php 
function get_dir_structure($path$recursive TRUE$ext FALSE) {
    
$return = array();
    if (
substr($path, -1) != '/') {
        
$path .= '/';
    }
    if (!
is_dir($path)) {
        
trigger_error('$path ' $path ' is not a directory!'E_USER_WARNING);
        return -
1;
    }
    if ((
$handle = @opendir($path)) === FALSE) {
        
print_r(error_get_last());
        
trigger_error('Unable to open directory!'E_USER_WARNING);
        return -
2;
    }
    while (
FALSE !== ($item readdir($handle))) {
        if (
$item == '.' || $item == '..')
            continue;
        if (
is_dir($path $item)) {
            if (
$recursive)
                
$return[$item] = get_dir_structure($path $item '/'TRUE$ext);
            else
                
$return[$item] = array();
        } else {
            if (
$ext != FALSE && strrpos($item$ext) !== FALSE)
                
$return[] = $item;
        }
    }
    
closedir($handle);
    return 
$return;
}
?>

Should be the final version.
Wow, your commenting is pure hell. Most of the comments you've made are entirely redundant. The only three that are remotely useful are:

<?php 
# A, possibly, recursive read of the directory sturcture.
# Check to see if we are going to read the contents of this directory.
# Check to see if we are filtering for one extension.
?>

Comments should only be used when the code is not easily self-explanatory.
I think you'll find its for people who wouldn't understand whats going on JamesF1
But that's entirely my point, the code tells you exactly what's going on. None of the other 'dumps' in this thread have such extensive and pointless commenting, which gave me the impression that those reading the thread should probably exhibit some form of PHP knowledge beforehand.

However, if you're going to make it that simple for those who don't understand, then giving an example of the output format and sample usage would also have been beneficial.
Quote from JamesF1 :Wow, your commenting is pure hell. Most of the comments you've made are entirely redundant. [...] Comments should only be used when the code is not easily self-explanatory.

Wow, thanks for the support there.

Quote from JamesF1 :However, if you're going to make it that simple for those who don't understand, then giving an example of the output format and sample usage would also have been beneficial.

Good point, I'll take that under advisement.

Quote from the_angry_angel :I think you'll find its for people who wouldn't understand whats going on JamesF1

Even better point, and that was my reason.
Quote from Dygear :Wow, thanks for the support there.

Don't get me wrong, I wasn't having a go at you - I just find commenting like that disruptive and and unreadable. But, as I said above, I wasn't aware of your reasons for doing it. So, if it caused any distress, I apologise

PHP Function Dump
(20 posts, started )
FGED GREDG RDFGDR GSFDG