$text, 'parsed_answers' => $answers, 'count' => count($answers) ]); return $answers; } /** * Parse a single answer letter from OCR text * * @param string $text OCR text from a single question's answer area * @return string|null The answer letter (A/B/C/D) or null if not found */ public function parseSingleAnswer(string $text): ?string { $text = trim($text); // Look for A, B, C, or D (case insensitive) if (preg_match('/[A-Da-d]/', $text, $matches)) { return strtoupper($matches[0]); } return null; } /** * Match parsed answers to questions * * @param array $questions Array of question data * @param array $answers Array of answers indexed by question number * @return array Updated questions with student_answer filled */ public function matchAnswersToQuestions(array $questions, array $answers): array { foreach ($questions as &$question) { $questionNumber = $question['question_number']; if (isset($answers[$questionNumber])) { $question['student_answer'] = $answers[$questionNumber]; } } return $questions; } }