3) { $name_patterns[] = "^" . $first_name . ".*"; } if ($last_name != $first_name && strlen($last_name)) { $name_patterns[] = "^" . $last_name . ".*"; } $regex_groups = array_map(function($x) { return "(?:" . $x . ")"; }, $name_patterns); $regex_pattern = "/" . implode("|", $regex_groups) . "/i"; $picture = ""; foreach ($mediaFiles as $mediaFile) { if ($mediaFile == ".") continue; if ($mediaFile == "..") continue; if (preg_match($regex_pattern, $mediaFile)) { $picture = $mediaFile; break; } } if ($picture === "") { print("\033[31mFailed to identify picture for " . $candidateName . "\033[0m\n"); } $rating = $data[2]; if ($rating == "score" || $rating == "") { // Don't include candidates who haven't been given a score continue; } $rating = (int)$rating; array_push( $candidates, [ "Ward" => $currentWard, "Candidate Name" => $candidateName, "Rating" => $rating, "Picture" => $picture ] ); } } fclose($handle); } else { error_log('Error opening input file'); exit(1); } if (empty($candidates)) { error_log("Failed to find any candidates"); exit(2); } if (($handle = fopen($outputFile, "w")) !== FALSE) { $headers = array( "Ward", "Candidate Name", "Rating", "Picture" ); if (fputcsv($handle, $headers) === FALSE) { error_log('Error writing headers to output file'); exit(3); } foreach ($candidates as $candidate) { $fields = array( $candidate["Ward"], $candidate["Candidate Name"], $candidate["Rating"], $candidate["Picture"] ); if (fputcsv($handle, $fields) === FALSE) { error_log('Error writing candidate to output file'); exit(3); } } } else { error_log('Error opening output file'); exit(1); } print("Data written to " . $outputFile . "\n"); exit(0);