View Single Post
Old 02-18-2009, 01:17 AM   #2 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Smile

You did well to come up with a concept For that good work by yourself, I have spent a little time writing you up a little function to make it happen. If you have any questions regarding the code, please ask. This way you will learn. I've tried to comment it as best as I could, however. I hope it helps you!

php Code:
function TalkPHP_Fetch_Latest_Articles($iLimit)
{
    /* Get all the files in the current directory ending in .html. */
    $aFiles = glob('*.html');
    $aPages = array();
   
    foreach ($aFiles as $szFile)
    {
        /* Construct two arrays to hold the items to sort below. */
        $aTimes[] = filemtime($szFile);
        $aPages[] = $szFile;
    }
   
    /* Sort the files by time modified descending, this will give us the order. */
    array_multisort($aTimes, SORT_DESC, $aPages, SORT_REGULAR, $aFiles);
    $aPages = array();
   
    /* Loop through the files in the correct order as changed above. */
    foreach ($aFiles as $iIndex => $szFile)
    {
        /* Break from the loop if we have the amount we desire. */
        if ($iIndex >= $iLimit)
        {
            break;
        }
       
        /* Get the file contents and use regular expressions to extract the title. */
        $szContent = file_get_contents($szFile);
       
        /* We are looking for the title between <title>This</title>. */
        preg_match('~<title.*?>(?P<title>.+?)</title>~is', $szContent, $aMatches);
       
        /* Place the location to the file and its title into an array. */
        $aPages[] = (object) array
        (
            'file' => $szFile,
            'title' => $aMatches['title']
        );
    }
   
    /* Return the pages limited to the amount we specified when we called the function. */
    return $aPages;
}

$aLatestArticles = TalkPHP_Fetch_Latest_Articles(5);

/* Loop through the pages and display them to the end user! */
foreach ($aLatestArticles as $pLatestArticle)
{
    printf(" - %s (%s)\n", $pLatestArticle->title, $pLatestArticle->file);
}
Attached Files
File Type: zip TalkPHP_Fetch_Latest_Articles.zip (1.8 KB, 15 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
buildakicker (02-19-2009)