@php $choiceQuestions = $questions['choice'] ?? []; $fillQuestions = $questions['fill'] ?? []; $answerQuestions = $questions['answer'] ?? []; $allQuestions = collect() ->concat($choiceQuestions) ->concat($fillQuestions) ->concat($answerQuestions) ->sortBy(fn ($q) => (int) ($q->question_number ?? 0)) ->values(); $normalizeQuickAnswer = function (?string $answer): string { $answer = trim((string) $answer); if ($answer === '') { return '—'; } $answer = preg_replace('/\s+/u', ' ', $answer) ?? $answer; $answer = str_replace([';', ';'], ' / ', $answer); return trim($answer); }; $quickAnswers = $allQuestions->map(function ($q) use ($normalizeQuickAnswer) { $answerText = $normalizeQuickAnswer($q->answer ?? ''); $isLong = mb_strlen(strip_tags($answerText)) > 20 || str_contains($answerText, "\n") || str_contains($answerText, "\r") || str_contains($answerText, '{') || str_contains($answerText, '\\frac') || str_contains($answerText, '见解析'); $processedAnswer = \App\Services\MathFormulaProcessor::processFormulas($answerText); // 速查统一为流式:移除显式换行 $processedAnswer = preg_replace('//iu', ' ', $processedAnswer) ?? $processedAnswer; return [ 'no' => (int) ($q->question_number ?? 0), 'answer' => $processedAnswer, 'long' => $isLong, 'extended' => false, ]; })->values(); $normalizeDetailHtml = function (?string $text): string { $text = trim((string) $text); if ($text === '') { return ''; } $text = preg_replace('/font-size\s*:[^;"]+;?/iu', '', $text) ?? $text; $text = preg_replace('/line-height\s*:[^;"]+;?/iu', '', $text) ?? $text; $text = preg_replace('/style\s*=\s*([\'"])\s*\1/iu', '', $text) ?? $text; return $text; }; $formatDetailForReadability = function (?string $text): string { $text = trim((string) $text); if ($text === '') { return ''; } // 仅做轻量分行:遇到“步骤X”时换行,避免整段拥挤 $text = preg_replace('/\s*(步骤\s*[0-9一二三四五六七八九十]+\s*[::])/u', '
$1', $text) ?? $text; $text = preg_replace('/\s*(第\s*[0-9一二三四五六七八九十]+\s*步\s*[::])/u', '
$1', $text) ?? $text; // 软换行点:分号后允许优先换行,缓解长公式串拥挤 $text = str_replace([';', ';'], [';', ';'], $text); // 轻量语义断点:在“则/故/所以”前给换行机会,避免整段公式堆在一行 $text = preg_replace('/,\s*(则|故|所以)/u', ',
$1', $text) ?? $text; // 清理重复换行 $text = preg_replace('/(?:
\s*){2,}/u', '
', $text) ?? $text; return ltrim($text, '
'); }; @endphp
答案速查
@foreach($quickAnswers as $item) {{ $item['no'] }}. {!! $item['answer'] !!} @endforeach
@foreach($allQuestions as $q) @php $no = (int) ($q->question_number ?? 0); $rawAnswer = trim((string) ($q->answer ?? '')); $rawSolution = $normalizeDetailHtml($q->solution ?? ''); $isSeeSolutionAnswer = (bool) preg_match('/^见\s*解析[。\.]?$|^详见解析/u', $rawAnswer); $renderAnswer = $rawAnswer !== '' ? \App\Services\MathFormulaProcessor::processFormulas($rawAnswer) : '—'; $renderSolution = $rawSolution !== '' ? \App\Services\MathFormulaProcessor::processFormulas($formatDetailForReadability($rawSolution)) : '(无详解)'; @endphp
{{ $no }}. @if(!$isSeeSolutionAnswer) {!! $renderAnswer !!} @endif 【解析】 {!! $renderSolution !!}
@endforeach