Example #1
0
 public function testReadLines()
 {
     $lines = readLines(__DIR__ . '/TestBag/bagit.txt', 'UTF-8');
     $this->assertEquals(2, count($lines));
     $this->assertEquals("BagIt-Version: 0.96", $lines[0]);
     $this->assertEquals("Tag-File-Character-Encoding: UTF-8", $lines[1]);
 }
    return $employee;
}
function formatArray($rows)
{
    foreach ($rows as $row) {
        $array = explode(', ', $row);
        $employees[] = formatEmployee($array);
    }
    return $employees;
}
function getPaddedString($piece1, $piece2, $piece3)
{
    $line = str_pad($piece1, 28) . str_pad($piece2, 50) . str_pad($piece3, 10) . PHP_EOL;
    return $line;
}
$contentsString = readLines('report.txt');
$rows = explode("\n", $contentsString);
$reportName = array_shift($rows);
$reportDate = array_shift($rows);
$officeName = array_shift($rows);
$rows = removeKruft($rows);
// Remove the existing column header names from the output
array_shift($rows);
$employeesArray = formatArray($rows);
$arrayOfSales = array_column($employeesArray, 'sales');
$arrayOfNames = array_column($employeesArray, 'name');
$arrayOfEmployeeNumbers = array_column($employeesArray, 'employee_number');
arsort($arrayOfSales);
$employeeSalesOrder = array_keys($arrayOfSales);
$totalSales = array_sum($arrayOfSales);
foreach ($employeeSalesOrder as $order) {
Example #3
0
 /**
  * This reads the data from the file name.
  *
  * @param string $fileName This is the file name to read. It defaults to 
  * the current value of the $fileName property. If given, it will set the 
  * value of the property also.
  *
  * @return array The data read.
  */
 public function read($fileName = null)
 {
     $this->_resetFileName($fileName);
     $manifest = array();
     $hashLen = $this->hashEncoding == 'sha1' ? 40 : 32;
     $lines = readLines($this->fileName, $this->fileEncoding);
     foreach ($lines as $line) {
         $hash = trim(substr($line, 0, $hashLen));
         $payload = trim(substr($line, $hashLen));
         if (strlen($payload) > 0) {
             $manifest[$payload] = $hash;
         }
     }
     $this->data = $manifest;
     return $manifest;
 }
Example #4
0
 /**
  * This reads the bag-info.txt file into an array dictionary.
  *
  * @return void
  */
 private function _readBagInfo()
 {
     try {
         $lines = readLines($this->bagInfoFile, $this->tagFileEncoding);
         $this->bagInfoData = BagIt_parseBagInfo($lines);
     } catch (Exception $exc) {
         array_push($this->bagErrors, array('baginfo', 'Error reading bag info file.'));
     }
 }
Example #5
0
 /**
  * This reads the data from the fetch file and populates the data array.
  *
  * @return array The data from the file.
  */
 public function read()
 {
     $lines = readLines($this->fileName, $this->fileEncoding);
     $fetch = array();
     foreach ($lines as $line) {
         $fields = preg_split('/\\s+/', $line);
         if (count($fields) == 3) {
             array_push($fetch, array('url' => $fields[0], 'length' => $fields[1], 'filename' => $fields[2]));
         }
     }
     $this->data = $fetch;
 }
<?php

$inputFile = 'movies.txt';
$outputFile = 'sorted_movies.txt';
function readLines($inputFile)
{
    $handle = fopen($inputFile, 'r');
    $contents = fread($handle, filesize($inputFile));
    $lines = explode(PHP_EOL, trim($contents));
    fclose($handle);
    return $lines;
}
function writeLines($outputFile, $lines)
{
    $handle = fopen($outputFile, 'w');
    fwrite($handle, implode(PHP_EOL, $lines));
    fclose($handle);
}
function randomMovie($movies)
{
    $key = array_rand($movies);
    return $movies[$key];
}
$movies = readLines($inputFile);
echo "Let's watch " . randomMovie($movies) . "!\n";
sort($movies);
writeLines($outputFile, $movies);