如何使此查找特定扩展名并仅显示文件名

1) how can i make this read only ".txt" files

2) how can i make it show only the file name so i can design it how i'd i like.. (<h1>$file</h1> for example)

$dir = "includes/news";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh)))
{
    if ($filename != "." && $filename != ".." && strtolower(substr($filename, strrpos($filename, '.') + 1)) == 'txt')
    {
    $files[] = $filename;
    }
}
sort($files);
echo $files;

what it shows right now is:

Array ( [0] => . [1] => .. [2] => [23.7] Hey.txt [3] => [24.7] New Website.txt [4] => [25.7] Example.txt )

Now, this is another way i could do it, i like it bit better:

        if( $handle = opendir( 'includes/news' )) 
    {
        while( $file = readdir( $handle )) 
        {
            if( strstr( $file, "txt" ) )
            {
                $addr = strtr($file, array('.txt' => ''));
                echo '<h1><a href="?module=news&read=' . $addr . '">&raquo; ' . $addr . "</a></h1>";
            }
         }
        closedir($handle);
    }

but the my issue with this one is that there is no Sorting between the files. everything is ok with the output, just the order of them. so if one of you can figure out how to sort them right, that would be perfect

Okay, try this:

$files = array();
if($handle = opendir( 'includes/news' )) {
    while( $file = readdir( $handle )) {
        if ($file != '.' && $file != '..') {
            // let's check for txt extension
            $extension = substr($file, -3);
            // filename without '.txt'
            $filename = substr($file, 0, -4);
            if ($extension == 'txt')
                $files[] = $file; // or $filename
        }
    }
    closedir($handle);
}
sort($files);
foreach ($files as $file)
    echo '<h1><a href="?module=news&read=' . $file 
        . '">&raquo; ' . $file . "</a></h1>";

I think this should accomplish what you want to do. It uses explode and a negative limit to find only .txt files and returns only the name.

$dir = "includes/news";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))){

    $fileName = explode('.txt', $node, -1)[0];
    if(count($fileName) )
        $files[] = '<h1>'.$fileName.'</h1>';

}

try to make it as simple as possible try this

function check_txt($file){
 $array = explode(".","$file");
 if(count($array)!=1 && $array[count($array)-1]=="txt"){return true;}
 return false;
  }
if($handle = opendir( 'includes/news' )) {
 while( $file = readdir( $handle )) 
    {
        if( check_txt($file) )
        {
            echo '<h1><a href="?module=news&read=' . $file . '">&raquo; ' . $file . "</a></h1>";
        }
     }
    closedir($handle);
}