I am using the following code to open a directory full of code, rename the files, feed it into an array, and sort it (see more below:
<?php
$handle = opendir('./code/') or die('Unable to open dir.');
$array1 = array();
echo "Available Scripts:<p>";
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "index.php" && $file != "index2.php") {
//add each file to an array so I can sort
$array1[] = $file;
}
}
closedir($handle);
//sort results of new array
$sortedarray = $array1;
sort($sortedarray);
//one last foreach loop, this time to display results of new array
foreach ( $sortedarray as $showcode ) {
echo "<a href=display.php?file=$showcode>";
$filename = (str_replace("_"," ",$showcode));
$filename = (str_replace("."," (",$filename));
echo $filename .")</a><br>\n";
}
?>
If the file is strip_extensions.php is will output: "strip extensions (php)"
I would like to pull the extension off of the files and use something to put small headers for the sections
for example:
echo "PHP code<br>";
if (the extension is PHP) {
write the link;
write the filename; }
THE PROBLEM:
The directory has files with the following extensions: .js, .php, .html, .script. That means I can't use the function I would have:
$extension = substr("filename", -3);
because the extension is an unknown number of characters. Anyone know how I can simply find the characters to the right of the last dot (which will be the only dot, incidentally) that is to say, replace that starred if statement above?
Thanks,
Adam