morrisonlevi

StackOverflow: PHP read data from several text files, output

Aug 24th, 2011
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.23 KB | None | 0 0
  1. <?php
  2.     //Answer to: http://stackoverflow.com/questions/7180128/php-read-data-from-several-text-files-outputting-a-certain-line-from-all-files/
  3.  
  4.     function readAfterColon($string) {
  5.         /**
  6.          * @param string $string The string you'd like to split on.
  7.          *
  8.          * @return string The data after the colon.  Multiple colons work just fine.
  9.          */
  10.         $array = explode(":",$string, 2);
  11.         return isset($array[1]) ? $array[1] : '';
  12.     }
  13.    
  14.     function readLine($lineNumber, $fileName) {
  15.         /**
  16.          * @param int $lineNumber The line number you want to read from the file.
  17.          * @param string $fileName The string representing the file you wish to open.
  18.          *
  19.          * @return string The contents of the line in the file.
  20.          */
  21.         $file = file($fileName);
  22.         return $file[$lineNumber-1];
  23.     }
  24.    
  25.     function readLineFrom($lineNumber, $fileNames) {
  26.         /**
  27.          * @param int $lineNumber The line number you want to read from the file.
  28.          * @param string|array $files Either a string for the file you want to open, or an array of filenames.
  29.          *
  30.          * @return array An associative array of the filename to the contents of the line.
  31.          */
  32.         $lines = array();
  33.         if(is_array($fileNames)) {
  34.             foreach($fileNames as $fileName) {
  35.                 $lines[$fileName] = readLine($lineNumber, $fileName);
  36.             }
  37.         } else {
  38.             $lines[$fileNames] = readLine($lineNumber, $fileNames);
  39.         }
  40.        
  41.         return $lines;
  42.     }
  43.    
  44.     function getFileNamesFromDirectory($directory = '.') {
  45.         /**
  46.          * @param string $directory The path to directory you'd like to get filenames from. Defaults to current directory.
  47.          *
  48.          * @return array An array of filenames.  Empty if there are no files.
  49.          */
  50.         $fileNames = array();
  51.         if ($handle = opendir($directory)) {
  52.             while (false !== ($file = readdir($handle))) {
  53.                 if ($file != "." && $file != "..") {
  54.                     $fileNames[] = $directory . $file;
  55.                 }
  56.             }
  57.             closedir($handle);
  58.         }
  59.         return $fileNames;
  60.     }
  61.    
  62.    
  63.     //get the line from every file
  64.     $descriptions = readLineFrom(5, getFileNamesFromDirectory('/var/www/test/'));
  65.    
  66.     //get the contents after the colon
  67.     foreach($descriptions as $fileName=>$description) {
  68.         $descriptions[$fileName] = readAfterColon($description);
  69.     }
  70.    
  71.     //display it
  72.     echo "<pre>\n";
  73.     print_r($descriptions);
  74.     echo "</pre>";
  75. ?>
Advertisement
Add Comment
Please, Sign In to add comment