0

以下のこのコードは、各ファイルのキーワードをエコーし​​ます。結果(各ファイルのキーワード)をアルファベット順に表示することは可能ですか。

<?php 
$files = (glob('{beauty,careers,education,entertainment,pets}/*.php', GLOB_BRACE)); 

    $selection = $files;
    $files = array();

    $keywords = $matches[1];

    foreach ($selection as $file) {    
    if (basename($file) == 'index.php') continue;
    if (basename($file) == 'error_log') continue;
    $files[] = $file;    
}
    foreach($files as $file) {
        $title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME));

    $content = file_get_contents($file);
    if (!$content) {

        echo "error reading file $file<br>";
    }
    else {
        preg_match("/keywords = \"(.*?)\"/i", $content, $matches);
        $keywords = $matches[1];
    }
        $results .= '<li><a href="http://domain.com/' . htmlentities($file, ENT_QUOTES) . '">'.$keywords.'</a></li>';    
}
?>
<?=$results?>
4

2 に答える 2

0

$keywords で sort() を使用します。いくつかのスペルミスと未使用の変数があります。すべてのファイルが処理されるまで、$results HTML を作成する必要はありません。そのため、ファイルを処理する foreach ループから $results = '' を移動し、並べ替え、キーワードを foreach して $results を作成します。

<?php 
$selection = (glob('{beauty,careers,education,entertainment,pets}/*.php', GLOB_BRACE)); 

$files = array();

// $keywords = $matches[1];

foreach ($selection as $file) {      
    if (basename($file) == 'index.php') continue;
    if (basename($file) == 'error_log') continue;
    $files[] = $file;      
} 

foreach($files as $file) { 
    //$title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME));

    $content = file_get_contents($file);
    if (!$content) { 

        echo "error reading file $file<br>";
    } 
    else { 
        preg_match("/keywords = \"(.*?)\"/i", $content, $matches);
        $keywords[] = $matches[1];
    } 
} 

$results = '';
sort($keywords); //comment this out to see the effects of the sort()
foreach($keywords as $_k) { 
    $results .= '<li><a href="http://domain.com/' . htmlentities($file, ENT_QUOTES) . '">'.$_k.'</a></li>';      
} 
?>
<?=$results?>
于 2013-01-26T16:46:00.900 に答える
0

sort()キーワード配列をソートするために使用します。

$keywords = array();

// ...

$keywords[] = $matches[1];
sort($keywords);
于 2013-01-26T16:35:06.167 に答える