Jump to content

Sorting with a Glob Function


Rineru

Recommended Posts

I'm using this script:

 

<?php
foreach(glob('*.swf') as $fn){
$t = str_replace("_","", $fn);
     echo '     <a href="' . $fn . '">' . ucfirst(substr($t, 0, -4)) . "</a><br>";
}
?>

 

 

To create a list of games, well it works.  GREAT... except one thing

 

I thought it would alphabetize the games automatically, it kinda does.

 

I want the script to do it, completely.

Link to comment
https://forums.phpfreaks.com/topic/73812-sorting-with-a-glob-function/
Share on other sites

what about this


<?php
$file = array();
foreach(glob('*.swf') as $fn)
{
$file[] = $fn;
}
sort($file);
foreach($file as $f)
{
$t = str_replace("_","", $f);
echo '     <a href="' . $f . '">' . ucfirst(substr($t, 0, -4)) . "</a><br>";
}
?>

 

Not prefect.. but works.. i rarely use glob..

Based off php.net

<?php

$_foo ='/server/public_html/path/';

function s_glob($dir){
$files = array();
if(is_dir($dir)){
    if($dh=opendir($dir)){
    while(($file = readdir($dh)) !== false){
        $files[]=$dir.$file;
    }}
}
return $files;
}

print_r(s_glob($_foo));

?>

is going to be faster than using the glob function.

but doesn't alphabetize all the output completely.

it looks like your running it on more than one folder..

 

as for businessman332211(php.net), function.. yes it returns an array..

 

your just need to add something like this to the returned data

sort($files);
foreach($files as $f)
{
$t = str_replace("_","", $f);
echo '     <a href="' . $f . '">' . ucfirst(substr($t, 0, -4)) . "</a><br>";
}
?>

Here is a sort function from php.net

 

<?php

 

$fruits = array("lemon", "orange", "banana", "apple");

sort($fruits);

reset($fruits);

while (list($key, $val) = each($fruits)) {

    echo "fruits[" . $key . "] = " . $val . "\n";

}

 

?>

 

http://jp2.php.net/manual/fi/function.sort.php

Here is a sort function from php.net

 

<?php

 

$fruits = array("lemon", "orange", "banana", "apple");

sort($fruits);

reset($fruits);

while (list($key, $val) = each($fruits)) {

    echo "fruits[" . $key . "] = " . $val . "\n";

}

 

?>

 

http://jp2.php.net/manual/fi/function.sort.php

 

I'm using the Glob function, not an array.  If you can tell me how to array files from a file list with no file extension and sorted, plus uppercase words.  I'll do it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.