Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.

View Poll Results: How stupid is Dave.

Voters
1. You may not vote on this poll
  • Not at all, Dave's a genius.

    0 0%
  • Only a little stupid, but we'll forgive him.

    0 0%
  • Pretty dense. Go eat your dinner with the other kids at the card table Dave.

    0 0%
  • Davey ain't never had no school larnin', but he got his self a Curious George book.

    1 100.00%
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sort and echo contents of multiple text files?

    Hello everyone,

    I'm trying to do something that I'm sure is very simple, but I'm having an unbelievably hard time doing it. I've even had some help from people online, but their suggestions haven't worked. I'm kind of new to PHP programming, but I'm starting to get the concept.

    Anyway, I want to read a directory full of text files and display the contents - sorted by the file name they came from. Here's an example...

    "Random phrase." in file-1.txt

    "Alphabet." in file-2.txt

    "Ship." in file-3.txt

    I'd read in the three files and echo...

    Random phrase.
    Alphabet.
    Ship.

    They're sorted by file name, not by file contents. The code I've got so far displays the file names in order, but I can't get the contents to display. Overall, I know how to sort and echo things, and I know how to echo the contents of text files, but I can't figure out how to do both. What you'll see here is just test code so I can sort out (pun intended) the problem.

    Code:
    <?php
    
    $dirFiles = array();
    if ($handle = opendir('./event-titles'))
    	{
    	while (false !== ($file = readdir($handle)))
    		{
    		if ($file != "." && $file != "..")
    			{
    			$dirFiles[] = $file;
    			}
    		}
    	closedir($handle);
    	}
    sort($dirFiles);
    foreach($dirFiles as $file)
    	{
    	$fileContents = file_get_contents($file);
    	echo $file."<br>".$fileContents."<br><br>";
    	}
    ?>
    I'm kind of stupid, so please be gentle. I'm trying to learn as I go, and I usually comment out code until I break something, then I go and put it back in. I tend to dissect code in order to learn it - I ain't too pretty durn good at book larnin'.

    Dave

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Read the contents into an array as well. I don't know whether these have multiple lines, nor if you need to keep track of the files they came from, so I'll just assume both.
    PHP Code:
    $aContents = array();
    foreach (
    $dirFiles AS $file)
    {
        if (
    $fp fopen($file'r'))
        {
            while (
    false !== ($line fgets($fp)))
            {
                
    $line trim($line);
                if (!isset(
    $aContents[$line]))
                {
                    
    $aContents[$line] = array();
                }
                
    $aContents[$line][] = $file;
            }
            
    fclose($fp);
        }
    }

    ksort($aContents);

    foreach (
    $aContents AS $phrase => $aFile)
    {
        
    printf('Phrase: %s found in files: %s'$phraseimplode(', '$aFile));

    Edit:
    BTW, if you don't care about file association, just file() all the files into an array_merge (if you need to deal with multiple lines), and sort that one.
    Last edited by Fou-Lu; 04-28-2012 at 04:32 AM.

  • #3
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Fou-Lu! This gives me a direction to follow. Much appreciated.

    Dave


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •