function getFirstLine($fileName, $testStr, $returnDelimiter = false)
{
    // opens a file, reads the headers, finds the delimiter using the test string, reads the first line under the headers, and forms an associative array.  Returns false if any step fails
    if (!is_file($fileName)) {
        return false;
    }
    $file = fopen($fileName, "r");
    $headerLine = fgets($file);
    $testLength = strlen($testStr);
    if ($headerLine === false) {
        return false;
    }
    $strpos = strpos($headerLine, $testStr);
    if ($strpos === false) {
        return false;
    }
    if (strlen($headerLine) === $testLength) {
        // this would mean that the file only has one header, the test string.  In this case, the delimiter doesn't matter, and every line is just the data for one field, $row[ $testString ]
        $delim = ',';
    } elseif ($strpos === 0) {
        // this means that the test string is the beginning of the first line, and we need to look at the character after the test string
        $delim = $headerLine[$testLength];
    } else {
        // finally, if the test string is not the start of the header line, we can just take the character before the start of the test string
        $delim = $headerLine[$strpos - 1];
    }
    $headers = str_getcsv($headerLine, $delim);
    if (!in_array($testStr, $headers)) {
        return false;
    }
    // this would mean that, for some reason, our delimiter failed to bring out our test string
    $data = fgetcsv($file, 0, $delim);
    fclose($file);
    if ($data === false) {
        return false;
    }
    if ($returnDelimiter) {
        return $delim;
    }
    return array_combine_safely($headers, $data);
}
示例#2
0
 if (!isset($fileMeta['files'])) {
     continue;
 }
 foreach ($fileMeta['files'] as $fileName) {
     $data = GetFromFile("{$path}/{$fileName}", false);
     $d = getFirstLine("{$path}/{$fileName}", $testHeader, true);
     if ($d === false) {
         continue;
     }
     $file = fopen("{$path}/{$fileName}", "r");
     $keys = fgetcsv($file, 0, $d);
     if ($category !== 'Final_Questions') {
         $extraFileMeta[$category]['Columns'] += array_flip($keys);
     }
     while (($line = fgetcsv($file, 0, $d)) !== false) {
         $row = array_combine_safely($keys, $line);
         if (!isset($IDs[$row['ID']])) {
             continue;
         }
         // if we don't have output from this person, we don't use any of their data
         if ($category === 'Final_Questions') {
             if (strtolower(trim($row['Type'])) === 'checkbox') {
                 if ($row['Response'] === '') {
                     continue;
                 }
                 $IDs[$row['ID']][$category][$row['Question'] . '_' . $row['Response']] = $row['Response'];
                 $extraFileMeta[$category]['Columns'][$row['Question'] . '_' . $row['Response']] = true;
             } else {
                 $IDs[$row['ID']][$category][$row['Question']] = $row['Response'];
                 $extraFileMeta[$category]['Columns'][$row['Question']] = true;
             }