PHP:反转链接被回显的顺序

时间:2012-03-09 22:40:41

标签: php

$archiveNumber = 0;
$archiveDirectory = 'blogtext/';
$archiveHandle = opendir( $archiveDirectory );
$entryAfter;
$entry;
if( $archiveHandle = opendir( 'blogtext/' ) )
{
    while( false !== ( $entry = readdir( $archiveHandle ) ) )
    {
        if( $entry != '.' && $entry != '..' && !is_dir( $entry ) && strpos( $entry, '-subject' ) ) 
        {
            $entryAfter = str_replace( '-'.$number.'-', '', $entry );
            $entryAfter = str_replace( '-subject', '', $entryAfter );
            $entryAfter = str_replace( '.txt', '', $entryAfter );
            echo '<a href="blog/'.strtolower( $entryAfter ).'.php"> ';
            include( 'blogtext/'.$entry );
            echo '</a><br>';
            $number++;
        }
    }
    closedir( $archiveHandle );
}

有没有办法扭转在此函数中回显链接的顺序?

此刻,链接以相反的顺序打印出来(以-biggestnumber-name.php开头,以-smallestnumber-name.php结尾)。我想扭转它。

(我知道我这样做的方式可能是头上迟钝的。你不需要提到它。)

2 个答案:

答案 0 :(得分:2)

不是在读取后直接回显它们,而是将所有链接收集到一个数组中,对该数组进行排序,然后遍历它。

答案 1 :(得分:1)

$archiveNumber = 0;
$archiveDirectory = 'blogtext/';
$archiveHandle = opendir( $archiveDirectory );
$entryAfter;
$entry;

$toEcho=""; //A string we'll echo later

if( $archiveHandle = opendir( 'blogtext/' ) )
{
    while( false !== ( $entry = readdir( $archiveHandle ) ) )
    {
        if( $entry != '.' && $entry != '..' && !is_dir( $entry ) && strpos( $entry, '-subject' ) ) 
        {
            $entryAfter = str_replace( '-'.$number.'-', '', $entry );
            $entryAfter = str_replace( '-subject', '', $entryAfter );
            $entryAfter = str_replace( '.txt', '', $entryAfter );
            ob_start();  //Start buffering echo
            echo '<a href="blog/'.strtolower( $entryAfter ).'.php"> ';
            include( 'blogtext/'.$entry );
            echo '</a><br>';
            $toEcho=ob_get_clean().$toEcho;  //Insert the link at the begining of $toEcho, that's where we reverse the echo order
            $number++;
        }
    }
    closedir( $archiveHandle );
}
echo $toEcho;  //echo the reversed list of links

请注意,我没有对此进行测试,但这个想法是可行的。